import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;

public class wrong_pm {

    public static void main(String[] args) throws FileNotFoundException {
        Scanner scanner = new Scanner(new File("wrong.in"));
        PrintWriter writer = new PrintWriter(new File("wrong.out"));

        int n = scanner.nextInt();
        int k = scanner.nextInt();

        boolean[] z = new boolean[n];
        int i = 0;
        int j = 0;
        while (!z[i]) {
            j++;
            z[i] = true;
            i = (i + k) % n;
        }

        writer.println(n - j);
        writer.close();
    }

}

