import javax.swing.plaf.basic.BasicInternalFrameTitlePane;
import  java.io.*;
import  java.math.*;
import java.nio.Buffer;
import java.sql.SQLSyntaxErrorException;
import  java.util.*;
import  java.text.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;


public class Main {
    public static boolean gcd(int a,int b){
        if(a==1||b==1){
            return false;
        }
        while(1>0){
            int t = a%b;
            if(t == 0){
                break;
            }
            else{
                a=b;
                b=t;
            }

        }
        if(b>1)	return true;
        else{
            return  false;
        }
    }

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        long n=cin.nextLong();
        long k=cin.nextLong();
        if(n<k){
            HashMap<Long,Integer> p = new HashMap<Long,Integer>();
            long x=n%k;
            long minn=x;
            while(true){
                x=Math.abs(x-k);
                if(!p.containsKey(x)){
                    p.put(x, 1);
                    minn = Math.min(x, minn);
                }
                else break;
            }
            System.out.println(minn);
        }
        else if(n%k==0){
            System.out.println("0");
        }
        else{
            System.out.println(Math.abs(n%k-k));
        }

    }
} import javax.swing.plaf.basic.BasicInternalFrameTitlePane; BasicInternalFrameTitlePane javax.swing.plaf.basic import  java.io.*; io.* java import  java.math.*; math.* java import java.nio.Buffer; Buffer java.nio import java.sql.SQLSyntaxErrorException; SQLSyntaxErrorException java.sql import  java.util.*; util.* java import  java.text.*; text.* java import java.util.regex.Pattern; Pattern java.util.regex import java.util.stream.Collectors; Collectors java.util.stream public class Main {
    public static boolean gcd(int a,int b){
        if(a==1||b==1){
            return false;
        }
        while(1>0){
            int t = a%b;
            if(t == 0){
                break;
            }
            else{
                a=b;
                b=t;
            }

        }
        if(b>1)	return true;
        else{
            return  false;
        }
    }

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        long n=cin.nextLong();
        long k=cin.nextLong();
        if(n<k){
            HashMap<Long,Integer> p = new HashMap<Long,Integer>();
            long x=n%k;
            long minn=x;
            while(true){
                x=Math.abs(x-k);
                if(!p.containsKey(x)){
                    p.put(x, 1);
                    minn = Math.min(x, minn);
                }
                else break;
            }
            System.out.println(minn);
        }
        else if(n%k==0){
            System.out.println("0");
        }
        else{
            System.out.println(Math.abs(n%k-k));
        }

    }
} Main public static boolean gcd(int a,int b){
        if(a==1||b==1){
            return false;
        }
        while(1>0){
            int t = a%b;
            if(t == 0){
                break;
            }
            else{
                a=b;
                b=t;
            }

        }
        if(b>1)	return true;
        else{
            return  false;
        }
    } gcd {
        if(a==1||b==1){
            return false;
        }
        while(1>0){
            int t = a%b;
            if(t == 0){
                break;
            }
            else{
                a=b;
                b=t;
            }

        }
        if(b>1)	return true;
        else{
            return  false;
        }
    } if(a==1||b==1){
            return false;
        } a==1||b==1 a==1 a 1 b==1 b 1 {
            return false;
        } return false; false while(1>0){
            int t = a%b;
            if(t == 0){
                break;
            }
            else{
                a=b;
                b=t;
            }

        } 1>0 1 0 {
            int t = a%b;
            if(t == 0){
                break;
            }
            else{
                a=b;
                b=t;
            }

        } int t = a%b; t a%b a b if(t == 0){
                break;
            }
            else{
                a=b;
                b=t;
            } t == 0 t 0 {
                break;
            } break; {
                a=b;
                b=t;
            } a=b a b b=t b t if(b>1)	return true;
        else{
            return  false;
        } b>1 b 1 return true; true {
            return  false;
        } return  false; false int a a int b b public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        long n=cin.nextLong();
        long k=cin.nextLong();
        if(n<k){
            HashMap<Long,Integer> p = new HashMap<Long,Integer>();
            long x=n%k;
            long minn=x;
            while(true){
                x=Math.abs(x-k);
                if(!p.containsKey(x)){
                    p.put(x, 1);
                    minn = Math.min(x, minn);
                }
                else break;
            }
            System.out.println(minn);
        }
        else if(n%k==0){
            System.out.println("0");
        }
        else{
            System.out.println(Math.abs(n%k-k));
        }

    } main {
        Scanner cin = new Scanner(System.in);
        long n=cin.nextLong();
        long k=cin.nextLong();
        if(n<k){
            HashMap<Long,Integer> p = new HashMap<Long,Integer>();
            long x=n%k;
            long minn=x;
            while(true){
                x=Math.abs(x-k);
                if(!p.containsKey(x)){
                    p.put(x, 1);
                    minn = Math.min(x, minn);
                }
                else break;
            }
            System.out.println(minn);
        }
        else if(n%k==0){
            System.out.println("0");
        }
        else{
            System.out.println(Math.abs(n%k-k));
        }

    } Scanner cin = new Scanner(System.in); cin new Scanner(System.in) long n=cin.nextLong(); n cin.nextLong() cin.nextLong cin long k=cin.nextLong(); k cin.nextLong() cin.nextLong cin if(n<k){
            HashMap<Long,Integer> p = new HashMap<Long,Integer>();
            long x=n%k;
            long minn=x;
            while(true){
                x=Math.abs(x-k);
                if(!p.containsKey(x)){
                    p.put(x, 1);
                    minn = Math.min(x, minn);
                }
                else break;
            }
            System.out.println(minn);
        }
        else if(n%k==0){
            System.out.println("0");
        }
        else{
            System.out.println(Math.abs(n%k-k));
        } n<k n k {
            HashMap<Long,Integer> p = new HashMap<Long,Integer>();
            long x=n%k;
            long minn=x;
            while(true){
                x=Math.abs(x-k);
                if(!p.containsKey(x)){
                    p.put(x, 1);
                    minn = Math.min(x, minn);
                }
                else break;
            }
            System.out.println(minn);
        } HashMap<Long,Integer> p = new HashMap<Long,Integer>(); p new HashMap<Long,Integer>() long x=n%k; x n%k n k long minn=x; minn x while(true){
                x=Math.abs(x-k);
                if(!p.containsKey(x)){
                    p.put(x, 1);
                    minn = Math.min(x, minn);
                }
                else break;
            } true {
                x=Math.abs(x-k);
                if(!p.containsKey(x)){
                    p.put(x, 1);
                    minn = Math.min(x, minn);
                }
                else break;
            } x=Math.abs(x-k) x Math.abs(x-k) Math.abs Math x-k x k if(!p.containsKey(x)){
                    p.put(x, 1);
                    minn = Math.min(x, minn);
                }
                else break; !p.containsKey(x) p.containsKey(x) p.containsKey p x {
                    p.put(x, 1);
                    minn = Math.min(x, minn);
                } p.put(x, 1) p.put p x 1 minn = Math.min(x, minn) minn Math.min(x, minn) Math.min Math x minn break; System.out.println(minn) System.out.println System.out System System.out minn if(n%k==0){
            System.out.println("0");
        }
        else{
            System.out.println(Math.abs(n%k-k));
        } n%k==0 n%k n k 0 {
            System.out.println("0");
        } System.out.println("0") System.out.println System.out System System.out "0" {
            System.out.println(Math.abs(n%k-k));
        } System.out.println(Math.abs(n%k-k)) System.out.println System.out System System.out Math.abs(n%k-k) Math.abs Math n%k-k n%k n k k String[] args args public class Main {
    public static boolean gcd(int a,int b){
        if(a==1||b==1){
            return false;
        }
        while(1>0){
            int t = a%b;
            if(t == 0){
                break;
            }
            else{
                a=b;
                b=t;
            }

        }
        if(b>1)	return true;
        else{
            return  false;
        }
    }

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        long n=cin.nextLong();
        long k=cin.nextLong();
        if(n<k){
            HashMap<Long,Integer> p = new HashMap<Long,Integer>();
            long x=n%k;
            long minn=x;
            while(true){
                x=Math.abs(x-k);
                if(!p.containsKey(x)){
                    p.put(x, 1);
                    minn = Math.min(x, minn);
                }
                else break;
            }
            System.out.println(minn);
        }
        else if(n%k==0){
            System.out.println("0");
        }
        else{
            System.out.println(Math.abs(n%k-k));
        }

    }
} public class Main {
    public static boolean gcd(int a,int b){
        if(a==1||b==1){
            return false;
        }
        while(1>0){
            int t = a%b;
            if(t == 0){
                break;
            }
            else{
                a=b;
                b=t;
            }

        }
        if(b>1)	return true;
        else{
            return  false;
        }
    }

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        long n=cin.nextLong();
        long k=cin.nextLong();
        if(n<k){
            HashMap<Long,Integer> p = new HashMap<Long,Integer>();
            long x=n%k;
            long minn=x;
            while(true){
                x=Math.abs(x-k);
                if(!p.containsKey(x)){
                    p.put(x, 1);
                    minn = Math.min(x, minn);
                }
                else break;
            }
            System.out.println(minn);
        }
        else if(n%k==0){
            System.out.println("0");
        }
        else{
            System.out.println(Math.abs(n%k-k));
        }

    }
} Main
import javax.swing.plaf.basic.BasicInternalFrameTitlePane;
import  java.io.*;
import  java.math.*;
import java.nio.Buffer;
import java.sql.SQLSyntaxErrorException;
import  java.util.*;
import  java.text.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;


public class Main {
    public static boolean gcd(int a,int b){
        if(a==1||b==1){
            return false;
        }
        while(1>0){
            int t = a%b;
            if(t == 0){
                break;
            }
            else{
                a=b;
                b=t;
            }

        }
        if(b>1)	return true;
        else{
            return  false;
        }
    }

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        long n=cin.nextLong();
        long k=cin.nextLong();
        if(n<k){
            HashMap<Long,Integer> p = new HashMap<Long,Integer>();
            long x=n%k;
            long minn=x;
            while(true){
                x=Math.abs(x-k);
                if(!p.containsKey(x)){
                    p.put(x, 1);
                    minn = Math.min(x, minn);
                }
                else break;
            }
            System.out.println(minn);
        }
        else if(n%k==0){
            System.out.println("0");
        }
        else{
            System.out.println(Math.abs(n%k-k));
        }

    }
}