let () =
  let rec read () =
    let (n,m) = Scanf.scanf " %d %d\n" (fun x y -> (x,y)) in
    if n = 0 && m = 0 then ()
    else
      begin
        let q = Queue.create () in
        for i = 1 to n do
          Queue.push i q
        done;
        let rec loop q cnt =
          if Queue.length q = 1 then
            print_endline (string_of_int (Queue.pop q))
          else if cnt mod m = 0 then
            let _ = Queue.pop q in loop q 1
          else
            begin Queue.push (Queue.pop q) q;loop q (cnt+1) end
        in loop q 1 ;read ()
      end
  in read ()
;;