program josephus(input,ouput);
uses Crt;

type link = ^node;
     node = record
     key: integer;
     next: link;
end;

var i, n, m: integer;
    t, x   : link;

begin
ClrScr;
read(n,m);
new(t); t^.key:=1; x:=t;

for i:=2 to n do
begin
 new(t^.next); t:=t^.next; t^.key:=i;
end;

t^.next:=x;

while t <> t^.next do
begin
 for i:=1 to m-1 do t:=t^.next;
 write(t^.next^.key);
 x:=t^.next;
 t^.next:=t^.next^.next;
 dispose(x);
end;

writeLN('Ende');
writeLN(t^.key);
readLN;
readLN;
end.