import java.util.Scanner;

public class Main{
 public static void main(String[] args){
   Scanner sc = new Scanner(System.in);
   long A = sc.nextLong();
   long B = sc.nextLong();
   long sum = A;
   long ans1 = 0;
   long ans2 = 0;

   boolean flag = true;
   if(A % B == 0) {
	   flag = false;
   }else {
	 sum = A % B;
   }

   if(flag == true) {
   ans1 = sum;
   ans2 = B - sum;
   }

   if(ans1 < ans2) {
   System.out.print(ans1);
   }else {
   System.out.print(ans2);
   }
   }
}

 import java.util.Scanner; Scanner java.util public class Main{
 public static void main(String[] args){
   Scanner sc = new Scanner(System.in);
   long A = sc.nextLong();
   long B = sc.nextLong();
   long sum = A;
   long ans1 = 0;
   long ans2 = 0;

   boolean flag = true;
   if(A % B == 0) {
	   flag = false;
   }else {
	 sum = A % B;
   }

   if(flag == true) {
   ans1 = sum;
   ans2 = B - sum;
   }

   if(ans1 < ans2) {
   System.out.print(ans1);
   }else {
   System.out.print(ans2);
   }
   }
} Main public static void main(String[] args){
   Scanner sc = new Scanner(System.in);
   long A = sc.nextLong();
   long B = sc.nextLong();
   long sum = A;
   long ans1 = 0;
   long ans2 = 0;

   boolean flag = true;
   if(A % B == 0) {
	   flag = false;
   }else {
	 sum = A % B;
   }

   if(flag == true) {
   ans1 = sum;
   ans2 = B - sum;
   }

   if(ans1 < ans2) {
   System.out.print(ans1);
   }else {
   System.out.print(ans2);
   }
   } main {
   Scanner sc = new Scanner(System.in);
   long A = sc.nextLong();
   long B = sc.nextLong();
   long sum = A;
   long ans1 = 0;
   long ans2 = 0;

   boolean flag = true;
   if(A % B == 0) {
	   flag = false;
   }else {
	 sum = A % B;
   }

   if(flag == true) {
   ans1 = sum;
   ans2 = B - sum;
   }

   if(ans1 < ans2) {
   System.out.print(ans1);
   }else {
   System.out.print(ans2);
   }
   } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long A = sc.nextLong(); A sc.nextLong() sc.nextLong sc long B = sc.nextLong(); B sc.nextLong() sc.nextLong sc long sum = A; sum A long ans1 = 0; ans1 0 long ans2 = 0; ans2 0 boolean flag = true; flag true if(A % B == 0) {
	   flag = false;
   }else {
	 sum = A % B;
   } A % B == 0 A % B A B 0 {
	   flag = false;
   } flag = false flag false {
	 sum = A % B;
   } sum = A % B sum A % B A B if(flag == true) {
   ans1 = sum;
   ans2 = B - sum;
   } flag == true flag true {
   ans1 = sum;
   ans2 = B - sum;
   } ans1 = sum ans1 sum ans2 = B - sum ans2 B - sum B sum if(ans1 < ans2) {
   System.out.print(ans1);
   }else {
   System.out.print(ans2);
   } ans1 < ans2 ans1 ans2 {
   System.out.print(ans1);
   } System.out.print(ans1) System.out.print System.out System System.out ans1 {
   System.out.print(ans2);
   } System.out.print(ans2) System.out.print System.out System System.out ans2 String[] args args public class Main{
 public static void main(String[] args){
   Scanner sc = new Scanner(System.in);
   long A = sc.nextLong();
   long B = sc.nextLong();
   long sum = A;
   long ans1 = 0;
   long ans2 = 0;

   boolean flag = true;
   if(A % B == 0) {
	   flag = false;
   }else {
	 sum = A % B;
   }

   if(flag == true) {
   ans1 = sum;
   ans2 = B - sum;
   }

   if(ans1 < ans2) {
   System.out.print(ans1);
   }else {
   System.out.print(ans2);
   }
   }
} public class Main{
 public static void main(String[] args){
   Scanner sc = new Scanner(System.in);
   long A = sc.nextLong();
   long B = sc.nextLong();
   long sum = A;
   long ans1 = 0;
   long ans2 = 0;

   boolean flag = true;
   if(A % B == 0) {
	   flag = false;
   }else {
	 sum = A % B;
   }

   if(flag == true) {
   ans1 = sum;
   ans2 = B - sum;
   }

   if(ans1 < ans2) {
   System.out.print(ans1);
   }else {
   System.out.print(ans2);
   }
   }
} Main
import java.util.Scanner;

public class Main{
 public static void main(String[] args){
   Scanner sc = new Scanner(System.in);
   long A = sc.nextLong();
   long B = sc.nextLong();
   long sum = A;
   long ans1 = 0;
   long ans2 = 0;

   boolean flag = true;
   if(A % B == 0) {
	   flag = false;
   }else {
	 sum = A % B;
   }

   if(flag == true) {
   ans1 = sum;
   ans2 = B - sum;
   }

   if(ans1 < ans2) {
   System.out.print(ans1);
   }else {
   System.out.print(ans2);
   }
   }
}


