import java.io.*;
import java.lang.*;
import java.util.*;
public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String[] str=br.readLine().split(" ");
        long N=Long.parseLong(str[0]);
        long K=Long.parseLong(str[1]);
        long ans=0;
        N=N%K;
        if(N==0) ans=0;
        else  ans=Math.min(K-N,N);
        System.out.println(ans);
    }
} import java.io.*; io.* java import java.lang.*; lang.* java import java.util.*; util.* java public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String[] str=br.readLine().split(" ");
        long N=Long.parseLong(str[0]);
        long K=Long.parseLong(str[1]);
        long ans=0;
        N=N%K;
        if(N==0) ans=0;
        else  ans=Math.min(K-N,N);
        System.out.println(ans);
    }
} Main public static void main(String[] args) throws IOException{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String[] str=br.readLine().split(" ");
        long N=Long.parseLong(str[0]);
        long K=Long.parseLong(str[1]);
        long ans=0;
        N=N%K;
        if(N==0) ans=0;
        else  ans=Math.min(K-N,N);
        System.out.println(ans);
    } main {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String[] str=br.readLine().split(" ");
        long N=Long.parseLong(str[0]);
        long K=Long.parseLong(str[1]);
        long ans=0;
        N=N%K;
        if(N==0) ans=0;
        else  ans=Math.min(K-N,N);
        System.out.println(ans);
    } BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) String[] str=br.readLine().split(" "); str br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " long N=Long.parseLong(str[0]); N Long.parseLong(str[0]) Long.parseLong Long str[0] str 0 long K=Long.parseLong(str[1]); K Long.parseLong(str[1]) Long.parseLong Long str[1] str 1 long ans=0; ans 0 N=N%K N N%K N K if(N==0) ans=0;
        else  ans=Math.min(K-N,N); N==0 N 0 ans=0 ans 0 ans=Math.min(K-N,N) ans Math.min(K-N,N) Math.min Math K-N K N N System.out.println(ans) System.out.println System.out System System.out ans String[] args args public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String[] str=br.readLine().split(" ");
        long N=Long.parseLong(str[0]);
        long K=Long.parseLong(str[1]);
        long ans=0;
        N=N%K;
        if(N==0) ans=0;
        else  ans=Math.min(K-N,N);
        System.out.println(ans);
    }
} public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String[] str=br.readLine().split(" ");
        long N=Long.parseLong(str[0]);
        long K=Long.parseLong(str[1]);
        long ans=0;
        N=N%K;
        if(N==0) ans=0;
        else  ans=Math.min(K-N,N);
        System.out.println(ans);
    }
} Main
import java.io.*;
import java.lang.*;
import java.util.*;
public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String[] str=br.readLine().split(" ");
        long N=Long.parseLong(str[0]);
        long K=Long.parseLong(str[1]);
        long ans=0;
        N=N%K;
        if(N==0) ans=0;
        else  ans=Math.min(K-N,N);
        System.out.println(ans);
    }
}