Problem:
Score : 300 points

Problem Statement

Given any integer x, Aoki can do the operation below.

Operation: Replace x with the absolute difference of x and K.

You are given the initial value of an integer N. Find the minimum possible value taken by N after Aoki does the operation zero or more times.

Constraints

0 ≤ N ≤ 10^{18}

1 ≤ K ≤ 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the minimum possible value taken by N after Aoki does the operation zero or more times.

Sample Input 1

7 4

Sample Output 1

1

Initially, N=7.

After one operation, N becomes |7-4| = 3.

After two operations, N becomes |3-4| = 1, which is the minimum value taken by N.

Sample Input 2

2 6

Sample Output 2

2

N=2 after zero operations is the minimum.

Sample Input 3

1000000000000000000 1

Sample Output 3

0

Solution in C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;

namespace ForATCODER
{
    public class Program
    {
        public static void Main(string[] args)
        {
            long[] nk = S2Larray(Console.ReadLine().Split(' '));
            long prev = nk[0];
            if (nk[0] < nk[1])
            {
                RESULT(nk[0]);
                return;
            }
            if (nk[0] % nk[1]==0)
            {
                RESULT(0);
                return;
            }
            while (true)
            {
               long next = Math.Abs(nk[0] - nk[1]);
                if(prev<next)
                {
                    RESULT(prev);
                    return;
                }
                else
                {
                    nk[0] = next;
                    prev = next;
                }
            }
         

        }
        public static bool swap(int x, int y, ref int[] array)
        {

            // check for out of range
            if (array.Length <= y || array.Length <= x) return false;


            // swap index x and y
            var buffer = array[x];
            array[x] = array[y];
            array[y] = buffer;


            return true;
        }
        static int arser(int[] ar ,int search, int[] except)
        {
            for(int i  = 0;i < ar.Length;i++)
            {
                if(ar[i] == search)
                {
                    if (Array.IndexOf(except, i) >= 0) continue;
                    return i;
                }
            }
            return -1;
        }

        static int s2i(String s)
        {
            return Convert.ToInt32(s);
        }
        static void RESULT(double res)
        {
            Console.WriteLine(res);
        }
        static void RESULT(long res)
        {
            Console.WriteLine(res);
        }
        static void RESULT(ulong res)
        {
            Console.WriteLine(res);
        }
        static void RESULT(int res)
        {
            Console.WriteLine(res);
        }
        static void RESULT(String res)
        {
            Console.WriteLine(res);
        }
        static String[] ReadLineAll(int count)
        {
            String[] res = new String[count];
            for (int i = 0; i < count; i++)
            {
                res[i] = Console.ReadLine();
            }
            return res;
        }

        static int[] S2Iarray(String[] src)
        {
            int len = src.Length;
            int[] res = new int[len];
            for (int i = 0; i < len; i++)
            {
                res[i] = Convert.ToInt32(src[i]);
            }
            return res;
        }
        static long[] S2Larray(String[] src)
        {
            int len = src.Length;
            long[] res = new long[len];
            for (int i = 0; i < len; i++)
            {
                res[i] = Convert.ToInt64(src[i]);
            }
            return res;
        }
    }
}