import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		long N = Long.parseLong(st.nextToken());
		long K = Long.parseLong(st.nextToken());
		
		long res1 = N % K;
		long res2 = K - res1;
		
		System.out.println(Math.min(res1, res2));

	}

} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io import java.util.StringTokenizer; StringTokenizer java.util public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		long N = Long.parseLong(st.nextToken());
		long K = Long.parseLong(st.nextToken());
		
		long res1 = N % K;
		long res2 = K - res1;
		
		System.out.println(Math.min(res1, res2));

	}

} Main public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		long N = Long.parseLong(st.nextToken());
		long K = Long.parseLong(st.nextToken());
		
		long res1 = N % K;
		long res2 = K - res1;
		
		System.out.println(Math.min(res1, res2));

	} main {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		long N = Long.parseLong(st.nextToken());
		long K = Long.parseLong(st.nextToken());
		
		long res1 = N % K;
		long res2 = K - res1;
		
		System.out.println(Math.min(res1, res2));

	} BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) StringTokenizer st = new StringTokenizer(br.readLine()); st new StringTokenizer(br.readLine()) long N = Long.parseLong(st.nextToken()); N Long.parseLong(st.nextToken()) Long.parseLong Long st.nextToken() st.nextToken st long K = Long.parseLong(st.nextToken()); K Long.parseLong(st.nextToken()) Long.parseLong Long st.nextToken() st.nextToken st long res1 = N % K; res1 N % K N K long res2 = K - res1; res2 K - res1 K res1 System.out.println(Math.min(res1, res2)) System.out.println System.out System System.out Math.min(res1, res2) Math.min Math res1 res2 String[] args args public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		long N = Long.parseLong(st.nextToken());
		long K = Long.parseLong(st.nextToken());
		
		long res1 = N % K;
		long res2 = K - res1;
		
		System.out.println(Math.min(res1, res2));

	}

} public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		long N = Long.parseLong(st.nextToken());
		long K = Long.parseLong(st.nextToken());
		
		long res1 = N % K;
		long res2 = K - res1;
		
		System.out.println(Math.min(res1, res2));

	}

} Main
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		long N = Long.parseLong(st.nextToken());
		long K = Long.parseLong(st.nextToken());
		
		long res1 = N % K;
		long res2 = K - res1;
		
		System.out.println(Math.min(res1, res2));

	}

}