import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt();
        int k = cin.nextInt();
        ListNode head = null;
        ListNode curr = null;
        ListNode help = null; 
        //生成约瑟夫环
        for (int i = 0; i < n; i++) {
            ListNode listNode = new ListNode(i);
            if (i == 0) {
                head = listNode;
                head.next = head;
                curr = head;
            } else {
                curr.next = listNode;
                listNode.next = head;
                curr = listNode;
            }
            if (i == n - 1) {
                help = curr;
            }
        }
        while (head != help) {
            for (int i = 0; i < k - 1; i++) {
                head = head.next;
                help = help.next;
            }
            head = head.next;
            help.next = head;
        }
        System.out.println(head.n);
    }

}

class ListNode
{
    int n;
    ListNode next;

    public ListNode(int i)
    {
        this.n = i;
    }
}