import java.io.*;
import java.util.*;
public class Main {
    static long gcd(long a, long b){ return (b==0) ? a : gcd(b, a%b); }
    static int gcd(int a, int b){ return (b==0) ? a : gcd(b, a%b); }
    public static void main(String args[]) {
        Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        long n = in.nextLong();
        long k = in.nextLong();
        long ans = gcd(n,k);
        System.out.println(find(n,k));
    }
    static long find(long n,long k){
        if(Math.abs(n-k)<n) return find((long)Math.min(n%k,k%n), k);
        return n;
    }
} import java.io.*; io.* java import java.util.*; util.* java public class Main {
    static long gcd(long a, long b){ return (b==0) ? a : gcd(b, a%b); }
    static int gcd(int a, int b){ return (b==0) ? a : gcd(b, a%b); }
    public static void main(String args[]) {
        Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        long n = in.nextLong();
        long k = in.nextLong();
        long ans = gcd(n,k);
        System.out.println(find(n,k));
    }
    static long find(long n,long k){
        if(Math.abs(n-k)<n) return find((long)Math.min(n%k,k%n), k);
        return n;
    }
} Main static long gcd(long a, long b){ return (b==0) ? a : gcd(b, a%b); } gcd { return (b==0) ? a : gcd(b, a%b); } return (b==0) ? a : gcd(b, a%b); (b==0) ? a : gcd(b, a%b) (b==0) b 0 a gcd(b, a%b) gcd b a%b a b long a a long b b static int gcd(int a, int b){ return (b==0) ? a : gcd(b, a%b); } gcd { return (b==0) ? a : gcd(b, a%b); } return (b==0) ? a : gcd(b, a%b); (b==0) ? a : gcd(b, a%b) (b==0) b 0 a gcd(b, a%b) gcd b a%b a b int a a int b b public static void main(String args[]) {
        Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        long n = in.nextLong();
        long k = in.nextLong();
        long ans = gcd(n,k);
        System.out.println(find(n,k));
    } main {
        Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        long n = in.nextLong();
        long k = in.nextLong();
        long ans = gcd(n,k);
        System.out.println(find(n,k));
    } Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in))); in new Scanner(new BufferedReader(new InputStreamReader(System.in))) long n = in.nextLong(); n in.nextLong() in.nextLong in long k = in.nextLong(); k in.nextLong() in.nextLong in long ans = gcd(n,k); ans gcd(n,k) gcd n k System.out.println(find(n,k)) System.out.println System.out System System.out find(n,k) find n k String args[] args static long find(long n,long k){
        if(Math.abs(n-k)<n) return find((long)Math.min(n%k,k%n), k);
        return n;
    } find {
        if(Math.abs(n-k)<n) return find((long)Math.min(n%k,k%n), k);
        return n;
    } if(Math.abs(n-k)<n) return find((long)Math.min(n%k,k%n), k); Math.abs(n-k)<n Math.abs(n-k) Math.abs Math n-k n k n return find((long)Math.min(n%k,k%n), k); find((long)Math.min(n%k,k%n), k) find (long)Math.min(n%k,k%n) Math.min Math n%k n k k%n k n k return n; n long n n long k k public class Main {
    static long gcd(long a, long b){ return (b==0) ? a : gcd(b, a%b); }
    static int gcd(int a, int b){ return (b==0) ? a : gcd(b, a%b); }
    public static void main(String args[]) {
        Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        long n = in.nextLong();
        long k = in.nextLong();
        long ans = gcd(n,k);
        System.out.println(find(n,k));
    }
    static long find(long n,long k){
        if(Math.abs(n-k)<n) return find((long)Math.min(n%k,k%n), k);
        return n;
    }
} public class Main {
    static long gcd(long a, long b){ return (b==0) ? a : gcd(b, a%b); }
    static int gcd(int a, int b){ return (b==0) ? a : gcd(b, a%b); }
    public static void main(String args[]) {
        Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        long n = in.nextLong();
        long k = in.nextLong();
        long ans = gcd(n,k);
        System.out.println(find(n,k));
    }
    static long find(long n,long k){
        if(Math.abs(n-k)<n) return find((long)Math.min(n%k,k%n), k);
        return n;
    }
} Main
import java.io.*;
import java.util.*;
public class Main {
    static long gcd(long a, long b){ return (b==0) ? a : gcd(b, a%b); }
    static int gcd(int a, int b){ return (b==0) ? a : gcd(b, a%b); }
    public static void main(String args[]) {
        Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        long n = in.nextLong();
        long k = in.nextLong();
        long ans = gcd(n,k);
        System.out.println(find(n,k));
    }
    static long find(long n,long k){
        if(Math.abs(n-k)<n) return find((long)Math.min(n%k,k%n), k);
        return n;
    }
}