/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
//package solution;

/**
 *
 * @author USER
 */

import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
import java.text.*;
public class Solution{
    static class InputReader {
        private final InputStream stream;
        private final byte[] buf = new byte[8192];
        private int curChar, snumChars;
        public InputReader(InputStream st) {
            this.stream = st;
        }
        public int read() {
            if (snumChars == -1)
                throw new InputMismatchException();
            if (curChar >= snumChars) {
                curChar = 0;
                try {
                    snumChars = stream.read(buf);
                }
                catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (snumChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }
        public int ni() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }
        public long nl() {
            int c = read();
            while (isSpaceChar(c)) {
            c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }
        public int[] nia(int n) {
            int a[] = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = ni();
            }
            return a;
        }
        public String rs() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                    c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }
        public String nextLine() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isEndOfLine(c));
            return res.toString();
        }
        public boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        private boolean isEndOfLine(int c) {
            return c == '\n' || c == '\r' || c == -1;
        }
    }
    public static class Key {

        private final int x;
        private final int y;

        public Key(int x, int y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (!(o instanceof Key)) return false;
            Key key = (Key) o;
            return x == key.x && y == key.y;
        }

        @Override
        public int hashCode() {
            int result = x;
            result = 31 * result + y;
            return result;
        }

    }

    static PrintWriter w = new PrintWriter(System.out);
    static long mod=998244353L,mod1=1000000007;
    static int co[][];
    static void dsort(){
        Arrays.parallelSort(co,(o1,o2)->o1[0]!=o2[0]?o1[0]-o2[0]:o1[1]-o2[1]);
    }
     static long  power(long x, long y) {
        long temp=0;
        if( y == 0)
            return 1;
        temp = power(x, y/2);
        if (y%2 == 0)
            return (temp*temp)%mod1;
        else
            return (((x*temp)%mod1)*temp)%mod1;
    }
    static long modInverse(long a, long m){
        long num=m;
        long x=1;
        long power=a%mod1;
        while(num>0){
              if(num%2==1){
                  x=(x*power)%mod1;
            }
            num>>=1;
            power=(power*power)%mod1;
        }
        return x;
    }
    static long ncrWithoutMod(int n,int r){
        if(n<r)return 0L;
        long ans=fact[n]/(fact[r]*fact[n-r]);
        return ans;
    }
    static long fact[]=new long[1000005];
    public static void findFact(){
        fact[0]=1;
        for(int i=1;i<=1000000;i++){
            fact[i]=(i*fact[i-1])%mod1;
        }
    }

    public static void main(String [] args){
        InputReader sdf=new InputReader(System.in);
        //Scanner sc=new Scanner(System.in);
       // BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                  int t=sdf.ni();
            next: while(t-->0){
               long a=sdf.nl();
               long b=sdf.nl();
               long i=0;
               if(a<b){
                   ;

               while(a!=b){
                   i++;
                   if(a+i>b){
                       b=b+i;
                   }
                   else a=a+i;

               }
               }
               else if(b<a){
                   long swap=a;
                   a=b;
                   b=swap;
                   //i=1;
                   while(a!=b){
                       i++;
                   if(a+i>b){
                       b=b+i;
                   }
                   else a=a+i;

                   //i++;

               }

               }
                System.out.println(i);

            }
        w.close();

}

//
//

      ///RKC

//    static long ncr(long n, long r)
//    {    if(r==0)return 1;
//            else {
//                long ans=n%M;
//                ans*=ncr(n-1,r-1)%M;
//                ans%=M;
//                ans*=inverse(r)%M;
//                ans%=M;return ans;
//
//    }
//
//    }
//    static long fast_exp(long x,long n){
//        long ans=1;
//        while(n>0){
//            if((n&1)!=0){
//                ans*=x;
//                ans%=M;
//            }
//            n=n>>1;
//            x=(x*x)%M;
//        }
//        return ans;
//    }
//static long inverse(long x){return fast_exp(x,M-2);}
//
//
//        ///RKC
//
//
//
//
//        static long bigpowmod(long a,long n,long m){
//            a=a%m;
//            long res=1;
//            while(n>0){
//                if((n&1)!=0){
//                    res=(res*a);
//                    res=res%m;
//                }
//                a=(a*a)%m;
//                n=n>>1;
//
//            }
//
//            return res;
//        }
//
//        static long printNcR(long n, long r) {
//
//        // p holds the value of n*(n-1)*(n-2)...,
//        // k holds the value of r*(r-1)...
//        long p = 1, k = 1;
//
//        // C(n, r) == C(n, n-r),
//        // choosing the smaller value
//        if (n - r < r) {
//            r = n - r;
//        }
//
//        if (r != 0) {
//            while (r > 0) {
//                p *= n;
//                k *= r;
//
//                // gcd of p, k
//                long m = __gcd(p, k);
//
//                // dividing by gcd, to simplify product
//                // division by their gcd saves from the overflow
//                p /= m;
//                k /= m;
//
//                n--;
//                r--;
//            }
//
//            // k should be simplified to 1
//            // as C(n, r) is a natural number
//            // (denominator should be 1 ) .
//        } else {
//            p = 1;
//        }
//
//        // if our approach is correct p = ans and k =1
//       return p;
//    }
//
//    static long __gcd(long n1, long n2) {
//        long gcd = 1;
//
//        for (int i = 1; i <= n1 && i <= n2; ++i) {
//            // Checks if i is factor of both integers
//            if (n1 % i == 0 && n2 % i == 0) {
//                gcd = i;
//            }
//        }
//        return gcd;
//    }
//        static long power(int x,int y)
//        {
//            long temp;
//            if( y == 0)
//                return 1;
//                temp = power(x, y/2);
//                if (y%2 == 0)
//                     return temp*temp;
//                    else
//                    return x*temp*temp;
//}

static class Reader
    {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public Reader()
        {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public Reader(String file_name) throws IOException
        {
            din = new DataInputStream(new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public String readLine() throws IOException
        {
            byte[] buf = new byte[64]; // line length
            int cnt = 0, c;
            while ((c = read()) != -1)
            {
                if (c == '\n')
                    break;
                buf[cnt++] = (byte) c;
            }
            return new String(buf, 0, cnt);
        }

        public int nextInt() throws IOException
        {
            int ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do
            {
                ret = ret * 10 + c - '0';
            }  while ((c = read()) >= '0' && c <= '9');

            if (neg)
                return -ret;
            return ret;
        }

        public long nextLong() throws IOException
        {
            long ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');
            if (neg)
                return -ret;
            return ret;
        }

        public double nextDouble() throws IOException
        {
            double ret = 0, div = 1;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();

            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');

            if (c == '.')
            {
                while ((c = read()) >= '0' && c <= '9')
                {
                    ret += (c - '0') / (div *= 10);
                }
            }

            if (neg)
                return -ret;
            return ret;
        }

        private void fillBuffer() throws IOException
        {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1)
                buffer[0] = -1;
        }

        private byte read() throws IOException
        {
            if (bufferPointer == bytesRead)
                fillBuffer();
            return buffer[bufferPointer++];
        }

        public void close() throws IOException
        {
            if (din == null)
                return;
            din.close();
        }
    }

//

class MySort {
    public void selectionSort(int[]a,int left,int right){
        //left index from 0 and right= last index
        for(int i=left;i<=right;i++){
            int min_index=i;
            for(int j=i+1;j<=right;j++){
                if(a[min_index]>a[j])min_index=j;
            }
            int temp=a[min_index];
            a[min_index]=a[i];
            a[i]=temp;
        }
    }

    public void bubbleSort(int a[],int  left,int right){
        //left index from 0 and right= last index

        for(int i=left;i<=right;i++){
            for(int j=left;j<right;j++){
                if(a[j+1]<a[j]){
                    int temp=a[j+1];
                    a[j+1]=a[j];
                    a[j]=temp;
                }
            }
        }
    }

    public void insertionSort(int a[],int left,int right){
        //left index from 0 and right= last index
        for(int i=left+1;i<=right;i++){
            int temp=a[i];
            int j=i-1;
            for(;j>=0&&temp<a[j];j--){
                a[j+1]=a[j];
            }
            a[j+1]=temp;
        }
    }

    public void quickSort(int a[],int left,int right){
        //left index from 0 and right= last index
        if(left<right){
            int pi_index=partition(a,left,right);

            quickSort(a,pi_index+1,right);//for left side of pivot
            quickSort(a,left,pi_index-1);//for right side of pivot

        }

    }
    private int partition(int a[],int left,int right){
            //last element is used as pivot
            int pivot=a[right];
            int i=left-1;
            for(int j=left;j<right;j++){
                if(pivot>a[j]){
                    i++;
                    int temp=a[i];
                    a[i]=a[j];
                    a[j]=temp;
                }
            }
            int temp=a[i+1];
            a[i+1]=a[right];
            a[right]=temp;
            return i+1;
    }

    public void mergeSort(int []a,int left,int right){
        if(left<right){
            int m=(left+right)/2;
            mergeSort(a, left, m);
            mergeSort(a, m+1, right);
            merging(a,left,m,right);

        }

    }
    private void merging(int[]a,int left,int m,int right){
        int n1=m-left+1;
        int n2=right-(m+1)+1;
        int []L=new int[n1];
        int []R=new int[n2];

        for(int i=0;i<n1;i++){L[i]=a[left+i];}
        for(int i=0;i<n2;i++){R[i]=a[m+1+i];}

        int i=0;int j=0;
        int k=left;
        while(i<n1&&j<n2){
            if(L[i]<=R[j]){
                a[k]=L[i];
                i++;
            }
            else {
                a[k]=R[j];
                j++;
            }
            k++;
        }

        while(i<n1){
            a[k]=L[i];
            i++;
            k++;
        }
        while(j<n2){
            a[k]=R[j];
            j++;
            k++;
        }
    }

}
}


import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.InputMismatchException;

public class B {
    final static int MOD = 1000000007;

    public static void main(String[] args) throws Exception {
        FastReader in = new FastReader(System.in);
        int t = in.nextInt();
        while (t-- > 0) {
            long a = in.nextLong();
            long b = in.nextLong();
            long n = findN(a, b);
            System.out.println(n);
        }
    }

    static long findN(long a, long b) {
        long d = Math.abs(a - b);
        double sqrt2d = Math.sqrt((double)d * 2D);
        long sqrt2dL = (long) sqrt2d;
        // N range from sqrt2dL - 2 to sqrt2dL + 2
        for (long i = sqrt2dL - 2; i <= sqrt2dL + 2; i++) {
            if (i < 0) continue;
            long iTriangular = i * (i + 1) / 2;
            if (iTriangular > d && (iTriangular - d) % 2 == 0) {
                return i;
            }
        }
        return sqrt2dL;
    }

    static class FastReader {
        private boolean finished = false;

        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private SpaceCharFilter filter;

        public FastReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }

            return buf[curChar++];
        }

        public int peek() {
            if (numChars == -1) {
                return -1;
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    return -1;
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c == ',') {
                    c = read();
                }
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public long nextLong() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public String nextString() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        private String readLine0() {
            StringBuilder buf = new StringBuilder();
            int c = read();
            while (c != '\n' && c != -1) {
                if (c != '\r') {
                    buf.appendCodePoint(c);
                }
                c = read();
            }
            return buf.toString();
        }

        public String nextLine() {
            String s = readLine0();
            while (s.trim().length() == 0)
                s = readLine0();
            return s;
        }

        public String nextLine(boolean ignoreEmptyLines) {
            if (ignoreEmptyLines) {
                return nextLine();
            } else {
                return readLine0();
            }
        }

        public BigInteger nextBigInteger() {
            try {
                return new BigInteger(nextString());
            } catch (NumberFormatException e) {
                throw new InputMismatchException();
            }
        }

        public char nextCharacter() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            return (char) c;
        }

        public double nextDouble() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            double res = 0;
            while (!isSpaceChar(c) && c != '.') {
                if (c == 'e' || c == 'E') {
                    return res * Math.pow(10, nextInt());
                }
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            }
            if (c == '.') {
                c = read();
                double m = 1;
                while (!isSpaceChar(c)) {
                    if (c == 'e' || c == 'E') {
                        return res * Math.pow(10, nextInt());
                    }
                    if (c < '0' || c > '9') {
                        throw new InputMismatchException();
                    }
                    m /= 10;
                    res += (c - '0') * m;
                    c = read();
                }
            }
            return res * sgn;
        }

        public boolean isExhausted() {
            int value;
            while (isSpaceChar(value = peek()) && value != -1)
                read();
            return value == -1;
        }

        public String next() {
            return nextString();
        }

        public SpaceCharFilter getFilter() {
            return filter;
        }

        public void setFilter(SpaceCharFilter filter) {
            this.filter = filter;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);
        }
    }
}


import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.InputMismatchException;

public class B {
    final static int MOD = 1000000007;

    public static void main(String[] args) throws Exception {
        FastReader in = new FastReader(System.in);
        int t = in.nextInt();
        while (t-- > 0) {
            long a = in.nextLong();
            long b = in.nextLong();
            long n = findN(a, b);
            System.out.println(n);
        }
    }

    static long findN(long a, long b) {
        long d = Math.abs(a - b);
        double sqrt2d = Math.sqrt((double)d * 2D);
        long sqrt2dL = (long) sqrt2d;
        // N range from sqrt2dL - 2 to sqrt2dL + 2
        for (long i = sqrt2dL - 2; i <= sqrt2dL + 2; i++) {
            if (i < 0) continue;
            long iTriangular = i * (i + 1) / 2;
            if (iTriangular >= d && (iTriangular - d) % 2 == 0) {
                return i;
            }
        }
        return sqrt2dL;
    }

    static class FastReader {
        private boolean finished = false;

        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private SpaceCharFilter filter;

        public FastReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }

            return buf[curChar++];
        }

        public int peek() {
            if (numChars == -1) {
                return -1;
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    return -1;
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c == ',') {
                    c = read();
                }
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public long nextLong() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public String nextString() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        private String readLine0() {
            StringBuilder buf = new StringBuilder();
            int c = read();
            while (c != '\n' && c != -1) {
                if (c != '\r') {
                    buf.appendCodePoint(c);
                }
                c = read();
            }
            return buf.toString();
        }

        public String nextLine() {
            String s = readLine0();
            while (s.trim().length() == 0)
                s = readLine0();
            return s;
        }

        public String nextLine(boolean ignoreEmptyLines) {
            if (ignoreEmptyLines) {
                return nextLine();
            } else {
                return readLine0();
            }
        }

        public BigInteger nextBigInteger() {
            try {
                return new BigInteger(nextString());
            } catch (NumberFormatException e) {
                throw new InputMismatchException();
            }
        }

        public char nextCharacter() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            return (char) c;
        }

        public double nextDouble() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            double res = 0;
            while (!isSpaceChar(c) && c != '.') {
                if (c == 'e' || c == 'E') {
                    return res * Math.pow(10, nextInt());
                }
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            }
            if (c == '.') {
                c = read();
                double m = 1;
                while (!isSpaceChar(c)) {
                    if (c == 'e' || c == 'E') {
                        return res * Math.pow(10, nextInt());
                    }
                    if (c < '0' || c > '9') {
                        throw new InputMismatchException();
                    }
                    m /= 10;
                    res += (c - '0') * m;
                    c = read();
                }
            }
            return res * sgn;
        }

        public boolean isExhausted() {
            int value;
            while (isSpaceChar(value = peek()) && value != -1)
                read();
            return value == -1;
        }

        public String next() {
            return nextString();
        }

        public SpaceCharFilter getFilter() {
            return filter;
        }

        public void setFilter(SpaceCharFilter filter) {
            this.filter = filter;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);
        }
    }
}


import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.InputMismatchException;

public class B {
    final static int MOD = 1000000007;

    public static void main(String[] args) throws Exception {
        FastReader in = new FastReader(System.in);
        int t = in.nextInt();
        while (t-- > 0) {
            long a = in.nextLong();
            long b = in.nextLong();
            long n = findN(a, b);
            System.out.println(n);
        }
    }

    static long findN(long a, long b) {
        long d = Math.abs(a - b);
        double sqrt2d = Math.sqrt((double)d * 2D);
        long sqrt2dL = (long) sqrt2d;
        // N range from sqrt2dL - 4 to sqrt2dL + 4
        for (long i = sqrt2dL - 4; i <= sqrt2dL + 4; i++) {
            if (i < 0) continue;
            long iTriangular = i * (i + 1) / 2;
            if (iTriangular >= d && (iTriangular - d) % 2 == 0) {
                return i;
            }
        }
        return sqrt2dL;
    }

    static class FastReader {
        private boolean finished = false;

        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private SpaceCharFilter filter;

        public FastReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }

            return buf[curChar++];
        }

        public int peek() {
            if (numChars == -1) {
                return -1;
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    return -1;
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c == ',') {
                    c = read();
                }
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public long nextLong() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public String nextString() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        private String readLine0() {
            StringBuilder buf = new StringBuilder();
            int c = read();
            while (c != '\n' && c != -1) {
                if (c != '\r') {
                    buf.appendCodePoint(c);
                }
                c = read();
            }
            return buf.toString();
        }

        public String nextLine() {
            String s = readLine0();
            while (s.trim().length() == 0)
                s = readLine0();
            return s;
        }

        public String nextLine(boolean ignoreEmptyLines) {
            if (ignoreEmptyLines) {
                return nextLine();
            } else {
                return readLine0();
            }
        }

        public BigInteger nextBigInteger() {
            try {
                return new BigInteger(nextString());
            } catch (NumberFormatException e) {
                throw new InputMismatchException();
            }
        }

        public char nextCharacter() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            return (char) c;
        }

        public double nextDouble() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            double res = 0;
            while (!isSpaceChar(c) && c != '.') {
                if (c == 'e' || c == 'E') {
                    return res * Math.pow(10, nextInt());
                }
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            }
            if (c == '.') {
                c = read();
                double m = 1;
                while (!isSpaceChar(c)) {
                    if (c == 'e' || c == 'E') {
                        return res * Math.pow(10, nextInt());
                    }
                    if (c < '0' || c > '9') {
                        throw new InputMismatchException();
                    }
                    m /= 10;
                    res += (c - '0') * m;
                    c = read();
                }
            }
            return res * sgn;
        }

        public boolean isExhausted() {
            int value;
            while (isSpaceChar(value = peek()) && value != -1)
                read();
            return value == -1;
        }

        public String next() {
            return nextString();
        }

        public SpaceCharFilter getFilter() {
            return filter;
        }

        public void setFilter(SpaceCharFilter filter) {
            this.filter = filter;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);
        }
    }
}


import java.io.*;
import java.util.*;

final public class B {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        int tst = Integer.parseInt(br.readLine());
        while(tst-->0){
            String[] str = br.readLine().split(" ");
            int a = Integer.parseInt(str[0]), b = Integer.parseInt(str[1]);
            int diff = Math.abs(a-b);
            int ans = 0;
            long sum = 0;
            for(int i = 0; i<=(int)1e7; i++){
                sum += i;
                long z = sum -diff;
                if(z>=0 && z%2 == 0){
                    ans = i;
                    break;
                }
            }
            sb.append(ans).append('\n');
        }
        System.out.println(sb);
    }
}


import java.io.*;
import java.util.*;
public class cdfer78b
{
	public static void main(String args[])throws Exception
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		int n=Integer.parseInt(br.readLine());
		for(int i=0;i<n;i++)
		{
			String s=br.readLine();
			String ss[]=s.split(" ");
			int a=Integer.parseInt(ss[0]);
			int b=Integer.parseInt(ss[1]);
			if(a>b)
			{
				int temp=a;
				a=b;
				b=temp;
			}
			int diff=b-a;
			double d1=(Math.sqrt(8*diff+1)-1)/2;
			int ans=0;
			//System.out.println(d1);
			if(d1-(int)d1>0)
			ans=(int)d1+1;
			else
			ans=(int)d1;
			int tempo=ans*(ans+1)/2;
			if(((tempo+a)-b)%2==0)
			System.out.println(ans);
			else
			System.out.println(ans+1);
		}
	}
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
public class cdfer78b
{
	public static void main(String args[])throws Exception
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		int n=Integer.parseInt(br.readLine());
		for(int i=0;i<n;i++)
		{
			String s=br.readLine();
			String ss[]=s.split(" ");
			long a=Long.parseLong(ss[0]);
			long b=Long.parseLong(ss[1]);
			if(a>b)
			{
				long temp=a;
				a=b;
				b=temp;
			}
			long diff=b-a;
			double d1=(Math.sqrt(8*diff+1)-1)/2;
			long ans=(long)Math.ceil(d1);
			//System.out.println(d1);
			//System.out.println(tempo);
			long tempo=ans*(ans+1)/2;
			while(((tempo+a)-b)%2!=0)
			{
				ans++;
				tempo=ans*(ans+1)/2;
			}
			System.out.println(ans);
		}
	}
}


import java.io.*;
import java.util.*;
public class cdfer78b
{
	public static void main(String args[])throws Exception
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		int n=Integer.parseInt(br.readLine());
		for(int i=0;i<n;i++)
		{
			String s=br.readLine();
			String ss[]=s.split(" ");
			long a=Long.parseLong(ss[0]);
			long b=Long.parseLong(ss[1]);
			if(a>b)
			{
				long temp=a;
				a=b;
				b=temp;
			}
			long diff=b-a;
			double d1=(Math.sqrt(8*diff+1)-1)/2;
			long ans=(long)Math.ceil(d1);
			//System.out.println(d1);
			long tempo=ans*(ans+1)/2;
			System.out.println(tempo);
			if(((tempo+a)-b)%2==0)
			System.out.println(ans);
			else
			System.out.println(ans+1);
		}
	}
}


import java.io.*;
import java.util.*;
public class cdfer78b
{
	public static void main(String args[])throws Exception
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		int n=Integer.parseInt(br.readLine());
		for(int i=0;i<n;i++)
		{
			String s=br.readLine();
			String ss[]=s.split(" ");
			long a=Long.parseLong(ss[0]);
			long b=Long.parseLong(ss[1]);
			if(a>b)
			{
				long temp=a;
				a=b;
				b=temp;
			}
			long diff=b-a;
			double d1=(Math.sqrt(8*diff+1)-1)/2;
			long ans=(int)Math.ceil(d1);
			//System.out.println(d1);
			long tempo=ans*(ans+1)/2;
			if(((tempo+a)-b)%2==0)
			System.out.println(ans);
			else
			System.out.println(ans+1);
		}
	}
}


import java.io.*;
import java.util.*;
public class cdfer78b
{
	public static void main(String args[])throws Exception
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		int n=Integer.parseInt(br.readLine());
		for(int i=0;i<n;i++)
		{
			String s=br.readLine();
			String ss[]=s.split(" ");
			long a=Long.parseLong(ss[0]);
			long b=Long.parseLong(ss[1]);
			if(a>b)
			{
				long temp=a;
				a=b;
				b=temp;
			}
			long diff=b-a;
			double d1=(Math.sqrt(8*diff+1)-1)/2;
			long ans=(long)Math.ceil(d1);
			//System.out.println(d1);
			long tempo=ans*(ans+1)/2;
			//System.out.println(tempo);
			if(((tempo+a)-b)%2==0)
			System.out.println(ans);
			else
			System.out.println(ans+1);
		}
	}
}


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

public class AandB {
    public static void main(String[] args) {
        FastReader input=new FastReader();
        int t=input.nextInt();
        while(t-->0)
        {
            long a=input.nextInt();
            long b=input.nextInt();
            if(a==b)
            {
                System.out.println(0);
            }
            else
            {
                long d=Math.abs(a-b);
                long n=(long)Math.ceil((Math.pow((1+8*(double)d),0.5)-1)/2);
                while(true)
                {
                    long s=n*(n+1)/2;
                    s+=d;
                    if(s%2==0)
                    {
                        break;
                    }
                    else
                    {
                        n++;
                    }
                }
                System.out.println(n);
            }
        }
    }
    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}


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

public class AandB {
    public static void main(String[] args) {
        FastReader input=new FastReader();
        int t=input.nextInt();
        while(t-->0)
        {
            long a=input.nextInt();
            long b=input.nextInt();
            if(a==b)
            {
                System.out.println(0);
            }
            else
            {
                long d=Math.abs(a-b);
                long n=(long)((Math.pow((1+8*(double)d),0.5)-1)/2);
                if(a<b)
                {
                    long a1=a+(n+1)*(n+2)/2;
                    a+=n*(n+1)/2;
                    long d1=b-a;
                    long d2=a1-b;
                    if(d1<=d2)
                    {
                        System.out.println(n+(d1*2));
                    }
                    else
                    {
                        System.out.println((n+1)+(d2*2));
                    }
                }
                else
                {
                    long b1=b+(n+1)*(n+2)/2;
                    b+=n*(n+1)/2;
                    long d1=a-b;
                    long d2=b1-a;
                    if(d1<=d2)
                    {
                        System.out.println(n+(d1*2));
                    }
                    else
                    {
                        System.out.println((n+1)+(d2*2));
                    }
                }
            }
        }
    }
    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}


import java.util.*;
public class Ab
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        for(int x=0;x<t;x++)
        {
            int a=sc.nextInt();
            int b=sc.nextInt();
            System.out.println((int)(-1+Math.sqrt(1-4*(a-b))/2+0.5));
        }
    }
}


import java.util.*;
public class V
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        for(int x=0;x<t;x++)
        {
            int a=Math.abs(sc.nextInt()-sc.nextInt());
            int q=(int)(-0.5+Math.sqrt(1+8*a)/2);
            while((q*(q+1))/2<a)
            {
                q++;
            }
            while(((q*(q+1))/2-a)%2==1)
            {
                q++;
            }
            System.out.println(q);
        }
        sc.close();
    }
}


import java.util.*;
public class V
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        for(int x=0;x<t;x++)
        {
            System.out.println((int)(0.999999999+Math.sqrt(1+8*Math.abs(sc.nextInt()-sc.nextInt()))/2));
        }
    }
}


import java.util.*;
public class V
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        for(int x=0;x<t;x++)
        {
            double q=(0.5+Math.sqrt(1+8*Math.abs(sc.nextInt()-sc.nextInt()))/2);
            System.out.println(Math.ceil(q));
        }
    }
}


import java.util.*;
public class V
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        for(int x=0;x<t;x++)
        {
            int a=sc.nextInt();
            int b=sc.nextInt();
            if(a>b)
            {
                int c=a;
                a=b;
                b=c;
            }
            int d=0;
            while(a<b)
            {
                d++;a+=d;
            }
            if(a-b<2&&a-b!=0)
            {
                a-=d;
                d--;
                System.out.println(d+(b-a)*2);
            }
            else if(a-b==0)
            {
                System.out.println(d);
            }
            else
            {
                System.out.println(d+2*((a-b)%2));
            }

        }
    }
}


import java.util.*;
public class V
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        for(int x=0;x<t;x++)
        {
            System.out.println((int)(1+Math.sqrt(1+8*(sc.nextInt()-sc.nextInt()))/2));
        }
    }
}


import java.util.*;
public class V
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        for(int x=0;x<t;x++)
        {
            int a=sc.nextInt();
            int b=sc.nextInt();
            if(a>b)
            {
                int c=a;
                a=b;
                b=c;
            }
            int d=0;
            while(a<b)
            {
                d++;a+=d;
            }
            a-=d;d--;
            System.out.println(d+(b-a)*2);

        }
    }
}


import java.util.*;
public class V
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        for(int x=0;x<t;x++)
        {
            double q=(-0.5+Math.sqrt(1+8*Math.abs(sc.nextInt()-sc.nextInt()))/2);
            System.out.println(Math.ceil(q));
        }
        sc.close();
    }
}


import java.util.*;
public class Ab
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        for(int x=0;x<t;x++)
        {
            int a=sc.nextInt();
            int b=sc.nextInt();
            int y=0;
            int g=1;
            while(a!=b)
            {
                if(a<b)
                {
                    a+=g;
                }
                else
                {
                    b+=g;
                }
            }
            System.out.println(y);
        }
    }
}


import java.util.*;
public class V
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        for(int x=0;x<t;x++)
        {
            System.out.println((int)(1+Math.sqrt(1+8*Math.abs(sc.nextInt()-sc.nextInt()))/2));
        }
    }
}


import java.util.*;
public class V
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        for(int x=0;x<t;x++)
        {
            int a=sc.nextInt();
            int b=sc.nextInt();
            if(a>b)
            {
                int c=a;
                a=b;
                b=c;
            }
            int d=0;
            while(a<b)
            {
                d++;a+=d;
            }
            if(a-b<2&&a-b!=0)
            {
                a-=d;
                d--;
                System.out.println(d+(b-a)*2);
            }
            else if(a-b==0)
            {
                System.out.println(d);
            }
            else
            {
                System.out.println(d+2*(a-b)%2);
            }

        }
    }
}


import java.util.*;
public class Ab
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        for(int x=0;x<t;x++)
        {
            int a=sc.nextInt();
            int b=sc.nextInt();
            System.out.println((int)(-1+Math.sqrt(1+Math.abs(4*(a-b)))/2+0.5));
        }
    }
}


import java.util.*;
public class V
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        for(int x=0;x<t;x++)
        {
            double q=(0.5+Math.sqrt(1+8*Math.abs(sc.nextInt()-sc.nextInt()))/2);
            if((10*q)%10==0)
            {
                q--;
            }
            System.out.println((int)(q));
        }
    }
}


import java.util.*;
public class Ab
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        for(int x=0;x<t;x++)
        {
            int a=sc.nextInt();
            int b=sc.nextInt();
            int y=0;
            int g=1;
            while(a!=b)
            {
                if(a<b)
                {
                    a+=g;
                }
                else
                {
                    b+=g;
                }
                y++;
            }
            System.out.println(y);
        }
    }
}


import java.util.*;
public class V
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        for(int x=0;x<t;x++)
        {
            double q=(0.5+Math.sqrt(1+8*Math.abs(sc.nextInt()-sc.nextInt()))/2);
            if(q!=(int)(q))
            {
                q++;
            }
            System.out.println((int)(q));
        }
    }
}


import java.util.*;
public class V
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        for(int x=0;x<t;x++)
        {
            int a=sc.nextInt();
            int b=sc.nextInt();
            if(a>b)
            {
                int c=a;
                a=b;
                b=c;
            }
            int d=0;
            while(a<b)
            {
                d++;a+=d;
            }
            if(a-b<2&&a-b!=0)
            {
                a-=d;
                d--;
                System.out.println(d+(b-a)*2);
            }
            else if(a-b==0)
            {
                System.out.println(d);
            }
            else
            {
                System.out.println(d+1);
            }

        }
    }
}


import java.util.*;
public class V
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        for(int x=0;x<t;x++)
        {
            int a=Math.abs(sc.nextInt()-sc.nextInt());
            int q=(int)(-0.5+Math.sqrt(1+8*a)/2);
            while((q*(q+1))/2<a)
            {
                q++;
            }
            if(((q*(q+1))/2)%2==1)
            {
                q++;
            }
            System.out.println(q);
        }
        sc.close();
    }
}


import java.util.*;
public class V
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        for(int x=0;x<t;x++)
        {
            int a=sc.nextInt();
            int b=sc.nextInt();
            if(a>b)
            {
                int c=a;
                a=b;
                b=c;
            }
            int d=0;
            while(a<b)
            {
                d++;a+=d;
            }
            a-=d;
            System.out.println(d+(b-a)*2);

        }
    }
}


import java.util.*;
public class V
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        for(int x=0;x<t;x++)
        {
            double q=(Math.sqrt(1+8*Math.abs(sc.nextInt()-sc.nextInt()))/2);
            if(q==(int)(q))
            {
                q--;
            }
            System.out.println((int)((1+Math.sqrt(1+8*Math.abs(sc.nextInt()-sc.nextInt()))/2)));
        }
    }
}


import java.util.*;
public class Ab
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        for(int x=0;x<t;x++)
        {
            int a=sc.nextInt();
            int b=sc.nextInt();
            int y=0;
            int g=1;
            while(a!=b)
            {
                if(a<b)
                {
                    a+=g;
                }
                else
                {
                    b+=g;
                }
                y++;g++;
            }
            System.out.println(y);
        }
    }
}


import java.util.*;
public class V
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        for(int x=0;x<t;x++)
        {
            int a=Math.abs(sc.nextInt()-sc.nextInt());
            int q=(int)(-0.5+Math.sqrt(1+8*a)/2);
            while((q*(q+1))/2<a)
            {
                q++;
            }
            if(((q*(q+1))/2-a)%2==1)
            {
                q++;
            }
            System.out.println(q);
        }
        sc.close();
    }
}


import java.util.*;
public class V
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        for(int x=0;x<t;x++)
        {
            double q=(0.5+Math.sqrt(1+8*Math.abs(sc.nextInt()-sc.nextInt()))/2);
            if(q==(int)(q))
            {
                q--;
            }
            System.out.println((int)(q));
        }
    }
}


import java.util.*;
public class Ab
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        for(int x=0;x<t;x++)
        {
            int a=sc.nextInt();
            int b=sc.nextInt();
            System.out.println((int)(-1+Math.sqrt(1-4*(a-b)))/2+0.5));
        }
    }
}


import java.util.*;
public class V
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        for(int x=0;x<t;x++)
        {
            int a=sc.nextInt();
            int b=sc.nextInt();
            if(a>b)
            {
                int c=a;
                a=b;
                b=c;
            }
            int d=0;
            while(a<b)
            {
                d++;a+=d;
            }
            if(a>b)
            {a-=d;d--;}
            System.out.println(d+(b-a)*2);

        }
    }
}


import java.util.*;
public class V
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        for(int x=0;x<t;x++)
        {
            int a=sc.nextInt();
            int b=sc.nextInt();
            if(a>b)
            {
                int c=a;
                a=b;
                b=c;
            }
            int d=0;
            while(a<b)
            {
                d++;a+=d;
            }
            if(a-b<2&&a-b!=0)
            {
                a-=d;
                d--;
                System.out.println(d+(b-a)*2);
            }
            else if(a-b==0)
            {
                System.out.println(d);
            }
            else
            {
                System.out.println(d);
            }

        }
    }
}


/***"In the name of Allah(swt), the most gracious, most merciful. Allah(swt) blesses with knowledge whom he wants."***/
/*** "ALHAMDULILLAH for Everything..." ***/
#include<bits/stdc++.h>
using namespace std;
#define  fastt       ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define  ll         long long
#define  pb         push_back
#define  mp         make_pair
#define  sz(a)      a.size()
#define  mem(a,b)   memset(a,b,sizeof(a))
#define  clr(x)     x.clear()
#define  ff         first
#define  ss         second
#define  case(x)    cout << "Case " << x <<": "

#define  md         (ll)1e9+7
#define  pi         acos(-1.0)
#define  cos(a)     cos(a*pi/180)
#define  sin(a)     sin(a*pi/180)
#define  tan(a)     tan(a*pi/180)
#define  cosi(a)    acos(a)/(pi/180)
#define  sini(a)    asin(a)/(pi/180)
#define  tani(a)    atan(a)/(pi/180)

#define  vi         vector <int>
#define  vl         vector <ll>
#define  vs         vector <string>
#define  vc         vector <char>
#define  vpii       vector < pair <int, int> >
#define  vpll       vector < pair <ll, ll> >
#define  vpsi       vector < pair <string, int> >
#define  vpis       vector < pair <int, string> >
#define  vpsl       vector < pair <string, ll> >
#define  vpls       vector < pair <ll, string> >

#define  pqi        priority_queue<int>
#define  pql        priority_queue<ll>
#define  minpqi     priority_queue<int,vector<int>,greater<int> >
#define  minpql     priority_queue<ll,vector<ll>,greater<ll> >

#define toint(a) atoll(a.c_str())
string tostr(int a) {stringstream rr;rr<<a;return rr.str();}
ll pow(ll c,ll d){return d==0?:c*pow(c,d-1);}
ll gcd(ll a,ll b) {return b==0? a:gcd(b,a%b);}
ll lcm(ll a,ll b) {return ((a*b)/gcd(a,b));}

/* for unordered map: mpp.max_load_factor(0.25); mpp.reserve(1024); */
/* cout << fixed << setprecision(12);*/
ll a[100005];
void calcu()
{
    ll i, j;
    a[0]=0;
    for(i=1; i<=100000; i++){
        a[i]=a[i-1]+i;
    }
}
int main()
{
    fastt;
    int t;
    cin>>t;
    calcu();
    while(t--){
        int aa, bb, i, j, k, m, mm, p, an;
        cin>>aa>>bb;
        if(aa>bb)swap(aa, bb);
        k=bb-aa;
        mm=k;
        mm=sqrt(mm*2);
        m=mm;
        if(a[m]>k)m--;

        if(a[m]==k){
            cout<<m<<endl;
        }
        else{
            if(k%2==1){
                an=m;
                j=0;
                for(i=m+1; ; i++){
                    j++;
                    if(a[i]%2==1)break;
                }
                cout<<an+j<<endl;
            }
            else{
                an=m;
                j=0;
                for(i=m+1; ; i++){
                    j++;
                    if(a[i]%2==0)break;
                }
                cout<<an+j<<endl;
            }
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef float fl;
typedef char ch;
typedef long long ll;
#define rep(i,a,n) for(i=a;i<n;i++)
#define rev(i,n,a) for(i=n;i>=a;i--)
#define repr(i,a,b) for(i=a;i<b;i++)
#define s(n) scanf("%d",&n);
#define s2(n,m) scanf("%d%d",&n,&m);
#define s3(n,m,o) scanf("%d%d%d",&n,&m,&o);
#define p(n) printf("%d",n);
#define p2(n,m) printf("%d %d",n,m);
#define sc(n) scanf("%c",&n);
#define sf(n) scanf("%f",&n);
#define sl(n) scanf("%lld",&n);
#define pc(n) printf("%c",n);
#define pf(n) printf("%f",n);
#define pl(n) printf("%lld",n);
#define br break;
#define co continue;

int main()
{
    int t;
    s(t)
    while(t--)
    {
        int a,b;
        s2(a,b)
        int x,y,z,i;
        x=abs(a-b);
        if(x==0)
        {
            printf("0\n");
            co
        }
        for(i=0;;i++)
        {
            y=i*(i+1)/2;
            if(y%2!=x%2)
                co
            else if(x>y)
                co
            else
            {
                z=i;
                br
            }
        }
        p(z)
        printf("\n");
    }
    return 0;
}


#include <bits/stdc++.h>
#define sum cnt *(cnt + 1) / 2
using namespace std;

inline int read() {
    int x = 0, f = 1;
    char ch = getchar();
    for (; ch < '0' || ch > '9'; ch = getchar())
        if (ch == '-') f = -1;
    for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
    return x * f;
}

int main() {
    for (int TTT = read(); TTT--;) {
        int a = abs(read() - read());
        int cnt = 0;
        for (; sum < a || (sum + a & 1); cnt++)
            ;
        printf("%d\n", cnt);
    }
    return 0;
}


var print = this.print || require('lol-io').print; //print --> outputs result and adds (enter)
var write = this.write || require('lol-io').write; //write --> creates output.txt with output
var readline = this.readline || require('lol-io').readline; //readline --> reads unread line of input.txt

var t, number, answer, action;

t = Number(readline());

for (i = 0; i < t; i++) {

    answer = 0;
    action = 1;
    number = readline().replace(/\r$/, '').split(' ').map(Number);

    while (number[0] != number[1]) {

        if (number[0] > number[1]) {

            if (number[0] - number[1] >= action) {
                number[1] += action;
            } else {
                number[0] += action;
            }
        } else {

            if ((number[1] - number[0]) >= action) {
                number[0] += action;
            } else {
                number[1] += action;
            }
        }

        action++;
        answer++;
    }

    print(answer);
}


var print = this.print || require('lol-io').print; //print --> outputs result and adds (enter)
var write = this.write || require('lol-io').write; //write --> creates output.txt with output
var readline = this.readline || require('lol-io').readline; //readline --> reads unread line of input.txt

var t, diff, sum, numbers;

t = Number(readline());

for (i = 0; i < t; i++) {           //loop cases

    numbers = readline().replace(/\r$/, '').split(' ').map(Number);
    diff = Math.abs(numbers[0] - numbers[1]);
    n = 0;
    sum = n * (n + 1) / 2;

    while(sum < diff || (sum % 2 != diff % 2)) {

        n++;
        sum = n * (n + 1) / 2;
    }

    print(n);
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
vector<int>v;
map<int,int>mp;
int main()
{
    ll a,b,i,j,x,y,n,t,m,sum,flag,z;
    string ah,bh;
    char str;

    scanf("%lld",&t);
    while(t--)
    {
        cin>>a>>b;

        if(a>b)
        {
            swap(a,b);
        }

            x = b-a;

            y = (sqrt(1+8*x)-1)/2;

            z = (y*(y+1))/2;
            if(z<x&&z+y+1==b-a)
            {
                printf("%lld\n",y+1);
            }
            else if(z<x)
            {
                printf("%lld\n",y+2);
            }
            else {
                printf("%lld\n",y);
            }

    }

return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
vector<int>v;
map<int,int>mp;
int main()
{
    int a,b,i,j,x,y,n,t,m,sum,flag,z;
    string ah,bh;
    char str;

    scanf("%d",&t);
    while(t--)
    {
        cin>>a>>b;

        if(a>b)
        {
            swap(a,b);
        }

        if(a==b)
        {
            printf("%d\n",0);
        }
        else if(b-a==2)
        {
            printf("3\n");
        }
        else if(b-a==3)
        {
            printf("2\n");
        }
        else{
            x = b-a;

            y = (sqrt(1+8*x)-1)/2;

            z = (y*(y+1))/2;
            if(z<x)
            {
                printf("%d\n",y+1);
            }
            else {
                printf("%d\n",y);
            }

        }

    }

return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
vector<int>v;
map<int,int>mp;
int main()
{
    ll a,b,i,j,x,y,n,t,m,sum,flag,z;
    string ah,bh;
    char str;

    scanf("%lld",&t);
    while(t--)
    {
        cin>>a>>b;

        if(a>b)
        {
            swap(a,b);
        }

            x = b-a;

            y = (sqrt(1+8*x)-1)/2;

            z = (y*(y+1))/2;
            if(z==x){
                printf("%lld\n",y);
            }
            else
            {
                y++;
                z = (y*(y+1))/2;
                if((a+z-b)%2==0)
                {
                    printf("%lld\n",y);
                }
                else{
                    printf("%d\n",y+1);
                }
            }

    }

return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
vector<int>v;
map<int,int>mp;
int main()
{
    int a,b,i,j,x,y,n,t,m,sum,flag,z;
    string ah,bh;
    char str;

    scanf("%d",&t);
    while(t--)
    {
        cin>>a>>b;

        if(a>b)
        {
            swap(a,b);
        }

        if(a==b)
        {
            printf("%d\n",0);
        }
        else if(b-a==2)
        {
            printf("3\n");
        }
        else if(b-a==3)
        {
            printf("2\n");
        }
        else{
            x = b-a;

            y = (sqrt(1+8*x)-1)/2;

            z = (y*(y+1))/2;
            if(z<x)
            {
                printf("%d\n",y+2);
            }
            else {
                printf("%d\n",y);
            }

        }

    }

return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
vector<int>v;
map<int,int>mp;
int main()
{
    ll a,b,i,j,x,y,n,t,m,sum,flag,z;
    string ah,bh;
    char str;

    scanf("%lld",&t);
    while(t--)
    {
        cin>>a>>b;

        if(a>b)
        {
            swap(a,b);
        }

            x = b-a;

            y = (sqrt(1+8*x)-1)/2;

            z = (y*(y+1))/2;
            if(z==x){
                printf("%lld\n",y);
            }
            else
            {
                y++;
                z = (y*(y+1))/2;
                if((z-x)%2==0)
                {
                    printf("%lld\n",y);
                }
                else{
                    printf("%d\n",y+1);
                }
            }

    }

return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
vector<int>v;
map<int,int>mp;
int main()
{
    ll a,b,i,j,x,y,n,t,m,sum,flag,z;
    string ah,bh;
    char str;

    scanf("%lld",&t);
    while(t--)
    {
        cin>>a>>b;

        if(a>b)
        {
            swap(a,b);
        }

            x = b-a;

            y = (sqrt(1+8*x)-1)/2;

            z = (y*(y+1))/2;
            if(z<x&&z+y+1==(b-a)+y+1)
            {
                printf("%lld\n",y+1);
            }
            else if(z<x)
            {
                printf("%lld\n",y+2);
            }
            else {
                printf("%lld\n",y);
            }

    }

return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
vector<int>v;
map<int,int>mp;
int main()
{
    int a,b,i,j,x,y,n,t,m,sum,flag;
    string ah,bh;
    char str;

    scanf("%d",&t);
    while(t--)
    {
        cin>>a>>b;

        if(a>b)
        {
            swap(a,b);
        }

        if(a==b)
        {
            printf("%d\n",0);
        }
        else{
            x = b-a;

            y = (sqrt(1+4*x)-1)/2;

            printf("%d\n",y+2);

        }

    }

return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
vector<int>v;
map<int,int>mp;
int main()
{
    ll a,b,i,j,x,y,n,t,m,sum,flag,z;
    string ah,bh;
    char str;

    scanf("%lld",&t);
    while(t--)
    {
        cin>>a>>b;

        if(a>b)
        {
            swap(a,b);
        }

        if(a==b)
        {
            printf("0\n");
        }
        else if(b-a==2)
        {
            printf("3\n");
        }
        else if(b-a==3)
        {
            printf("2\n");
        }
        else{
            x = b-a;

            y = (sqrt(1+8*x)-1)/2;

            z = (y*(y+1))/2;
            if(z<x)
            {
                printf("%lld\n",y+1);
            }
            else {
                printf("%lld\n",y);
            }

        }

    }

return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
vector<int>v;
map<int,int>mp;
int main()
{
    ll a,b,i,j,x,y,n,t,m,sum,flag,z;
    string ah,bh;
    char str;

    scanf("%lld",&t);
    while(t--)
    {
        cin>>a>>b;

        if(a>b)
        {
            swap(a,b);
        }

            x = b-a;

            y = (sqrt(1+8*x)-1)/2;

            z = (y*(y+1))/2;
            if(z<x)
            {
                printf("%lld\n",y+2);
            }
            else {
                printf("%lld\n",y);
            }

    }

return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
vector<int>v;
map<int,int>mp;
int main()
{
    ll a,b,i,j,x,y,n,t,m,sum,flag,z;
    string ah,bh;
    char str;

    scanf("%lld",&t);
    while(t--)
    {
        cin>>a>>b;

        if(a>b)
        {
            swap(a,b);
        }

        x = b-a;

        y = (sqrt(1+8*x)-1)/2;

        z = (y*(y+1))/2;
        if(z==x)
        {
            printf("%lld\n",y);
        }
        else
        {
            y++;
            z = (y*(y+1))/2;
            if((z-x)%2==0)
            {
                printf("%lld\n",y);
            }
            else
            {
                y++;
                z = (y*(y+1))/2;
                if((z-x)%2==0)
                {
                    printf("%lld\n",y);
                }
                else
                {
                    printf("%lld\n",y+1);
                }
            }
        }

    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
vector<int>v;
map<int,int>mp;
int main()
{
    ll a,b,i,j,x,y,n,t,m,sum,flag,z;
    string ah,bh;
    char str;

    scanf("%lld",&t);
    while(t--)
    {
        cin>>a>>b;

        if(a>b)
        {
            swap(a,b);
        }

        if(a==b)
        {
            printf("0\n");
        }
        else if(b-a==2)
        {
            printf("3\n");
        }
        else if(b-a==3)
        {
            printf("2\n");
        }
        else{
            x = b-a;

            y = (sqrt(1+8*x)-1)/2;

            z = (y*(y+1))/2;
            if(z<x)
            {
                printf("%lld\n",y+2);
            }
            else {
                printf("%lld\n",y);
            }

        }

    }

return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pp push_back
#define ff first
#define ss second

using namespace std;

ll n,m,i,j,x,a,b,c,z,t,aa,bb,k,y[1000001];
string s,ss;
map <char, ll> mp,mpp;
int main()
{
    ios::sync_with_stdio(0);
    for(i=1;(i*(i+1))/2<=1e9;i++)
    {
        y[i]=(i*(i+1))/2;
    }
    n=i;
    cin >> t;
    while(t--)
    {
        cin >> a >> b;
        z=0;
        c=max(a,b)-min(a,b);
        a=lower_bound(y,y+n,c)-y;
        if(y[a]-c>=c-y[a-1] && a>0)
        {
            a--;
        }
        z=a+((max(y[a],c)-min(y[a],c))*2);
        cout << z << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pp push_back
#define ff first
#define ss second

using namespace std;

ll n,m,i,j,x,a,b,c,z,t,aa,bb,k,y[1000001];
string s,ss;
map <char, ll> mp,mpp;
int main()
{
    ios::sync_with_stdio(0);
    for(i=1;(i*(i+1))/2<=1e9;i++)
    {
        y[i]=(i*(i+1))/2;
    }
    n=i;
    cin >> t;
    while(t--)
    {
        cin >> a >> b;
        z=0;
        c=max(a,b)-min(a,b);
        a=lower_bound(y,y+n,c)-y;
        if(y[a]-c>=c-y[a-1] && a>0)
        {
            a--;
        }
        c-=y[a];
        if(c<0)
            c*=-1;
        z=a+(c*2);
        for(i=1;i<z;i++)
        {
            c+=y[i];
            if(y[i+1]==c)
            {
                z=min(z,i+1);
            }
        }
        cout << z << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pp push_back
#define ff first
#define ss second

using namespace std;

ll n,m,i,j,x,a,b,c,z,t,aa,bb,k,y[1000001];
string s,ss;
map <char, ll> mp,mpp;
int main()
{
    ios::sync_with_stdio(0);
    for(i=1; (i*(i+1))/2<=1e10; i++)
    {
        y[i]=(i*(i+1))/2;
    }
    n=i;
    cin >> t;
    while(t--)
    {
        cin >> a >> b;
        c=max(a,b)-min(a,b);
        a=lower_bound(y,y+n,c)-y;
        if((y[a]-c)%2==1 && a>=c)
            a++;
        else if((y[a]-c)%2==1)
            a+=2;
        cout << a << endl;
    }
    return 0;
}


// NA
#include <bits/stdc++.h>
#define BUFFER 5
#define BUFF(x) x + BUFFER
#define mp make_pair
// #pragma GCC optimize "O4"
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
const int N_MAX = 2e5;
int n, k;
ll ans;
int inp[BUFF(N_MAX)];
struct S {
  int val, cnt;
  ll cost;
};
vector<S> mod_inp, left_surf, right_surf;
void solve() {
  sort(inp + 1, inp + 1 + n);
  for (int i = 1; i <= n; ++i) {
    if (i == 1 || mod_inp.back().val != inp[i])
      mod_inp.push_back({ inp[i], 0, 0 });
    ++mod_inp.back().cnt;
    if (mod_inp.back().cnt == k) {
      ans = 0;
      return;
    }
  }
  ll t_cost;
  for (int i = 0, t_cnt; i < mod_inp.size(); ++i) {
    t_cnt = mod_inp[i].cnt;
    t_cost = 0;
    if (i) {
      auto prv = left_surf.back();
      t_cnt += prv.cnt;
      t_cost = prv.cost + 1LL * prv.cnt * abs(mod_inp[i].val - prv.val);
    }
    left_surf.push_back({ mod_inp[i].val, t_cnt, t_cost });
  }
  for (int i = mod_inp.size() - 1, t_cnt; i >= 0; --i) {
    t_cnt = mod_inp[i].cnt;
    t_cost = 0;
    if (i != mod_inp.size() - 1) {
      auto prv = right_surf.back();
      t_cnt += prv.cnt;
      t_cost = prv.cost + 1LL * prv.cnt * abs(prv.val - mod_inp[i].val);
    }
    right_surf.push_back({ mod_inp[i].val, t_cnt, t_cost });
  }
  reverse(right_surf.begin(), right_surf.end());
  S lft, rght;
  ll s_l_cost, s_r_cost;
  for (int i = 0, t_left, t_right, req; i < mod_inp.size(); ++i) {
    if (i == 0)
      lft = { 0, 0, 0 };
    else lft = left_surf[i - 1];
    if (i == mod_inp.size() - 1)
      rght = { 0, 0, 0 };
    else rght = right_surf[i + 1];
    s_l_cost = abs(mod_inp[i].val - lft.val);
    s_r_cost = abs(mod_inp[i].val - rght.val);
    // give preference to left
    req = k - mod_inp[i].cnt;
    t_left = min(req, lft.cnt);
    req -= t_left;
    t_right = min(req, rght.cnt);
    if (!t_left)
      lft.cost = 0;
    if (!t_right)
      rght.cost = 0;
    if (mod_inp[i].cnt + t_left + t_right == k)
      ans = min(ans, lft.cost + (1LL * t_left * s_l_cost) + rght.cost + (1LL * t_right * s_r_cost));
    // give preference to right
    req = k - mod_inp[i].cnt;
    t_right = min(req, rght.cnt);
    req -= t_right;
    t_left = min(req, lft.cnt);
    if (!t_left)
      lft.cost = 0;
    if (!t_right)
      rght.cost = 0;
    if (mod_inp[i].cnt + t_left + t_right == k)
      ans = min(ans, lft.cost + (1LL * t_left * s_l_cost) + rght.cost + (1LL * t_right * s_r_cost));
  }
}
void program() {
  cin >> n >> k;
  ans = LONG_LONG_MAX;
  for (int i = 1; i <= n; ++i)
    cin >> inp[i];
  solve();
  cout << ans << endl;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  program();
  return 0;
}


// NA
#include <bits/stdc++.h>
#define BUFFER 5
#define BUFF(x) x + BUFFER
#define mp make_pair
// #pragma GCC optimize "O4"
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
const int N_MAX = 2e5;
int n, k;
ll ans;
int inp[BUFF(N_MAX)];
struct S {
  int val, cnt;
  ll cost;
};
vector<S> mod_inp, left_surf, right_surf;
void solve() {
  sort(inp + 1, inp + 1 + n);
  for (int i = 1; i <= n; ++i) {
    if (!mod_inp.size() || mod_inp.back().val != inp[i])
      mod_inp.push_back({ inp[i], 0, 0 });
    ++mod_inp.back().cnt;
    if (mod_inp.back().cnt == k) {
      ans = 0;
      return;
    }
  }
  ll t_cost;
  for (int i = 0, t_cnt; i < mod_inp.size(); ++i) {
    t_cnt = mod_inp[i].cnt;
    t_cost = 0;
    if (left_surf.size()) {
      auto prv = left_surf.back();
      t_cnt += prv.cnt;
      t_cost = prv.cost + 1LL * prv.cnt * abs(mod_inp[i].val - prv.val);
    }
    left_surf.push_back({ mod_inp[i].val, t_cnt, t_cost });
  }
  for (int i = mod_inp.size() - 1, t_cnt; i >= 0; --i) {
    t_cnt = mod_inp[i].cnt;
    t_cost = 0;
    if (right_surf.size()) {
      auto prv = right_surf.back();
      t_cnt += prv.cnt;
      t_cost = prv.cost + 1LL * prv.cnt * abs(mod_inp[i].val - prv.val);
    }
    right_surf.push_back({ mod_inp[i].val, t_cnt, t_cost });
  }
  reverse(right_surf.begin(), right_surf.end());
  S lft, rght;
  ll s_l_cost, s_r_cost, a, b;
  for (int i = 0, t_left, t_right, req; i < mod_inp.size(); ++i) {
    if (i == 0)
      lft = { 0, 0, 0 };
    else lft = left_surf[i - 1];
    if (i == mod_inp.size() - 1)
      rght = { 0, 0, 0 };
    else rght = right_surf[i + 1];
    s_l_cost = abs(mod_inp[i].val - lft.val);
    s_r_cost = abs(mod_inp[i].val - rght.val);
    // give preference to left
    req = k - mod_inp[i].cnt;
    t_left = min(req, lft.cnt);
    req -= t_left;
    t_right = min(req, rght.cnt);
    if (mod_inp[i].cnt + t_left + t_right == k) {
      a = (t_left) ? lft.cost + (1LL * t_left * s_l_cost) : 0;
      b = (t_right) ? rght.cost + (1LL * t_right * s_r_cost) : 0;
      ans = min(ans, a + b);
    }
    // give preference to right
    req = k - mod_inp[i].cnt;
    t_right = min(req, rght.cnt);
    req -= t_right;
    t_left = min(req, lft.cnt);
    if (mod_inp[i].cnt + t_left + t_right == k) {
      a = (t_left) ? lft.cost + (1LL * t_left * s_l_cost) : 0;
      b = (t_right) ? rght.cost + (1LL * t_right * s_r_cost) : 0;
      ans = min(ans, a + b);
    }
  }
}
void program() {
  cin >> n >> k;
  ans = LONG_LONG_MAX;
  for (int i = 1; i <= n; ++i)
    cin >> inp[i];
  solve();
  cout << ans << endl;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  program();
  return 0;
}


// NA
#include <bits/stdc++.h>
#define BUFFER 5
#define BUFF(x) x + BUFFER
#define mp make_pair
// #pragma GCC optimize "O4"
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
const int N_MAX = 2e5;
int n, k;
ll ans;
int inp[BUFF(N_MAX)];
struct S {
  int val, cnt;
  ll cost;
};
vector<S> mod_inp, left_surf, right_surf;
void solve() {
  sort(inp + 1, inp + 1 + n);
  for (int i = 1; i <= n; ++i) {
    if (i == 1 || mod_inp.back().val != inp[i])
      mod_inp.push_back({ inp[i], 0, 0 });
    ++mod_inp.back().cnt;
    if (mod_inp.back().cnt == k) {
      ans = 0;
      return;
    }
  }
  ll t_cost;
  for (int i = 0, t_cnt; i < mod_inp.size(); ++i) {
    t_cnt = mod_inp[i].cnt;
    t_cost = 0;
    if (i) {
      auto prv = left_surf.back();
      t_cnt += prv.cnt;
      t_cost = prv.cost + 1LL * prv.cnt * abs(mod_inp[i].val - prv.val);
    }
    left_surf.push_back({ mod_inp[i].val, t_cnt, t_cost });
  }
  for (int i = mod_inp.size() - 1, t_cnt; i >= 0; --i) {
    t_cnt = mod_inp[i].cnt;
    t_cost = 0;
    if (i != mod_inp.size() - 1) {
      auto prv = right_surf.back();
      t_cnt += prv.cnt;
      t_cost = prv.cost + 1LL * prv.cnt * abs(prv.val - mod_inp[i].val);
    }
    right_surf.push_back({ mod_inp[i].val, t_cnt, t_cost });
  }
  reverse(right_surf.begin(), right_surf.end());
  S lft, rght;
  ll s_l_cost, s_r_cost;
  for (int i = 0, t_left, t_right, req; i < mod_inp.size(); ++i) {
    if (i == 0)
      lft = { 0, 0, 0 };
    else lft = left_surf[i - 1];
    if (i == mod_inp.size() - 1)
      rght = { 0, 0, 0 };
    else rght = right_surf[i + 1];
    s_l_cost = abs(mod_inp[i].val - lft.val);
    s_r_cost = abs(mod_inp[i].val - rght.val);
    // give preference to left
    req = k - mod_inp[i].cnt;
    t_left = min(req, lft.cnt);
    req -= t_left;
    t_right = req;
    if (!t_left)
      lft.cost = 0;
    if (!t_right)
      rght.cost = 0;
    ans = min(ans, lft.cost + (1LL * t_left * s_l_cost) + rght.cost + (1LL * t_right * s_r_cost));
    // give preference to right
    req = k - mod_inp[i].cnt;
    t_right = min(req, rght.cnt);
    req -= t_right;
    t_left = req;
    if (!t_left)
      lft.cost = 0;
    if (!t_right)
      rght.cost = 0;
    ans = min(ans, lft.cost + (1LL * t_left * s_l_cost) + rght.cost + (1LL * t_right * s_r_cost));
  }
}
void program() {
  cin >> n >> k;
  ans = LONG_LONG_MAX;
  for (int i = 1; i <= n; ++i)
    cin >> inp[i];
  solve();
  cout << ans << endl;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  program();
  return 0;
}


// NA
#include <bits/stdc++.h>
#define BUFFER 5
#define BUFF(x) x + BUFFER
#define mp make_pair
// #pragma GCC optimize "O4"
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
const int N_MAX = 2e5;
int n, k;
ll ans;
int inp[BUFF(N_MAX)];
struct S {
  int val, cnt;
  ll cost;
};
vector<S> mod_inp, left_surf, right_surf;
void solve() {
  sort(inp + 1, inp + 1 + n);
  for (int i = 1; i <= n; ++i) {
    if (!mod_inp.size() || mod_inp.back().val != inp[i])
      mod_inp.push_back({ inp[i], 0, 0 });
    ++mod_inp.back().cnt;
    if (mod_inp.back().cnt == k) {
      ans = 0;
      return;
    }
  }
  ll t_cost;
  for (int i = 0, t_cnt; i < mod_inp.size(); ++i) {
    t_cnt = mod_inp[i].cnt;
    t_cost = 0;
    if (left_surf.size()) {
      auto prv = left_surf.back();
      t_cnt += prv.cnt;
      t_cost = prv.cost + 1LL * prv.cnt * abs(mod_inp[i].val - prv.val);
    }
    left_surf.push_back({ mod_inp[i].val, t_cnt, t_cost });
  }
  for (int i = mod_inp.size() - 1, t_cnt; i >= 0; --i) {
    t_cnt = mod_inp[i].cnt;
    t_cost = 0;
    if (right_surf.size()) {
      auto prv = right_surf.back();
      t_cnt += prv.cnt;
      t_cost = prv.cost + 1LL * prv.cnt * abs(mod_inp[i].val - prv.val);
    }
    right_surf.push_back({ mod_inp[i].val, t_cnt, t_cost });
  }
  reverse(right_surf.begin(), right_surf.end());
  S lft, rght;
  ll s_l_cost, s_r_cost, a, b;
  for (int i = 0, t_left, t_right, req; i < mod_inp.size(); ++i) {
    if (i == 0)
      lft = { 0, 0, 0 };
    else lft = left_surf[i - 1];
    if (i == (mod_inp.size() - 1))
      rght = { 0, 0, 0 };
    else rght = right_surf[i + 1];
    s_l_cost = lft.cost + (1LL * lft.cnt * abs(mod_inp[i].val - 1 - lft.val));
    s_r_cost = rght.cost + (1LL * rght.cnt * abs(mod_inp[i].val + 1 - rght.val));
    req = k - mod_inp[i].cnt;
    t_left = min(req, lft.cnt);
    req -= t_left;
    t_right = req;
    a = t_left ? (s_l_cost + t_left) : 0;
    b = t_right ? (s_r_cost + t_right) : 0;
    ans = min(ans, a + b);
    req = k - mod_inp[i].cnt;
    t_right = min(req, rght.cnt);
    req -= t_right;
    t_left = req;
    a = t_left ? (s_l_cost + t_left) : 0;
    b = t_right ? (s_r_cost + t_right) : 0;
    ans = min(ans, a + b);
  }
}
void program() {
  cin >> n >> k;
  ans = LONG_LONG_MAX;
  for (int i = 1; i <= n; ++i)
    cin >> inp[i];
  solve();
  cout << ans << endl;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  program();
  return 0;
}


// NA
#include <bits/stdc++.h>
#define BUFFER 5
#define BUFF(x) x + BUFFER
#define mp make_pair
// #pragma GCC optimize "O4"
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
const int N_MAX = 2e5;
int n, k;
ll ans;
int inp[BUFF(N_MAX)];
struct S {
  int val, cnt;
  ll cost;
};
vector<S> mod_inp, left_surf, right_surf;
void solve() {
  sort(inp + 1, inp + 1 + n);
  for (int i = 1; i <= n; ++i) {
    if (i == 1 || mod_inp.back().val != inp[i])
      mod_inp.push_back({ inp[i], 0, 0 });
    ++mod_inp.back().cnt;
    if (mod_inp.back().cnt == k) {
      ans = 0;
      return;
    }
  }
  ll t_cost;
  for (int i = 0, t_cnt; i < mod_inp.size(); ++i) {
    t_cnt = mod_inp[i].cnt;
    t_cost = 0;
    if (i) {
      auto prv = left_surf.back();
      t_cnt += prv.cnt;
      t_cost = prv.cost + 1LL * prv.cnt * (mod_inp[i].val - prv.val);
    }
    left_surf.push_back({ mod_inp[i].val, t_cnt, t_cost });
    if (t_cnt == k)
      ans = min(ans, t_cost);
  }
  for (int i = mod_inp.size() - 1, t_cnt; i >= 0; --i) {
    t_cnt = mod_inp[i].cnt;
    t_cost = 0;
    if (i != mod_inp.size() - 1) {
      auto prv = right_surf.back();
      t_cnt += prv.cnt;
      t_cost = prv.cost + 1LL * prv.cnt * (prv.val - mod_inp[i].val);
    }
    right_surf.push_back({ mod_inp[i].val, t_cnt, t_cost });
    if (t_cnt == k)
      ans = min(ans, t_cost);
  }
  reverse(right_surf.begin(), right_surf.end());
  S lft, rght;
  ll s_l_cost, s_r_cost;
  for (int i = 0, t_left, t_right, req; i < mod_inp.size(); ++i) {
    if (i == 0)
      lft = { 0, 0, 0 };
    else lft = left_surf[i - 1];
    if (i == mod_inp.size() - 1)
      rght = { 0, 0, 0 };
    else rght = right_surf[i + 1];
    s_l_cost = abs(mod_inp[i].val - lft.val);
    s_r_cost = abs(mod_inp[i].val - rght.val);
    t_left = t_right = 0;
    req = k - mod_inp[i].cnt;
    if (lft.cost + s_l_cost * req < rght.cost + s_r_cost * req) {
      t_left = min(req, lft.cnt);
      req -= t_left;
      t_right = req;
    } else {
      t_right = min(req, rght.cnt);
      req -= t_right;
      t_left = req;
    }
    ans = min(ans, lft.cost + (1LL * t_left * s_l_cost) + rght.cost + (1LL * t_right * s_r_cost));
  }
}
void program() {
  cin >> n >> k;
  ans = LONG_LONG_MAX;
  for (int i = 1; i <= n; ++i)
    cin >> inp[i];
  solve();
  cout << ans << endl;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  program();
  return 0;
}


// NA
#include <bits/stdc++.h>
#define BUFFER 5
#define BUFF(x) x + BUFFER
#define mp make_pair
// #pragma GCC optimize "O4"
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
const int N_MAX = 2e5;
int n, k;
ll ans;
int inp[BUFF(N_MAX)];
struct S {
  int val, cnt;
  ll cost;
};
vector<S> mod_inp, left_surf, right_surf;
void solve() {
  sort(inp + 1, inp + 1 + n);
  for (int i = 1; i <= n; ++i) {
    if (i == 1 || mod_inp.back().val != inp[i])
      mod_inp.push_back({ inp[i], 0, 0 });
    ++mod_inp.back().cnt;
    if (mod_inp.back().cnt == k) {
      ans = 0;
      return;
    }
  }
  ll t_cost;
  for (int i = 0, t_cnt; i < mod_inp.size(); ++i) {
    t_cnt = mod_inp[i].cnt;
    t_cost = 0;
    if (i) {
      auto prv = left_surf.back();
      t_cnt += prv.cnt;
      t_cost = prv.cost + 1LL * prv.cnt * (mod_inp[i].val - prv.val);
    }
    left_surf.push_back({ mod_inp[i].val, t_cnt, t_cost });
    if (t_cnt == k)
      ans = min(ans, t_cost);
  }
  for (int i = mod_inp.size() - 1, t_cnt; i >= 0; --i) {
    t_cnt = mod_inp[i].cnt;
    t_cost = 0;
    if (i != mod_inp.size() - 1) {
      auto prv = right_surf.back();
      t_cnt += prv.cnt;
      t_cost = prv.cost + 1LL * prv.cnt * (prv.val - mod_inp[i].val);
    }
    right_surf.push_back({ mod_inp[i].val, t_cnt, t_cost });
    if (t_cnt == k)
      ans = min(ans, t_cost);
  }
  reverse(right_surf.begin(), right_surf.end());
  S lft, rght;
  ll s_l_cost, s_r_cost;
  for (int i = 0, t_left, t_right, req; i < mod_inp.size(); ++i) {
    if (i == 0)
      lft = { 0, 0, 0 };
    else lft = left_surf[i - 1];
    if (i == mod_inp.size() - 1)
      rght = { 0, 0, 0 };
    else rght = right_surf[i + 1];
    s_l_cost = abs(mod_inp[i].val - lft.val);
    s_r_cost = abs(mod_inp[i].val - rght.val);
    t_left = t_right = 0;
    req = k - mod_inp[i].cnt;
    t_left = min(req, lft.cnt);
    req -= t_left;
    t_right = req;
    ans = min(ans, lft.cost + (1LL * t_left * s_l_cost) + rght.cost + (1LL * t_right * s_r_cost));
    req = k - mod_inp[i].cnt;
    t_right = min(req, rght.cnt);
    req -= t_right;
    t_left = req;
    ans = min(ans, lft.cost + (1LL * t_left * s_l_cost) + rght.cost + (1LL * t_right * s_r_cost));
  }
}
void program() {
  cin >> n >> k;
  ans = LONG_LONG_MAX;
  for (int i = 1; i <= n; ++i)
    cin >> inp[i];
  solve();
  cout << ans << endl;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  program();
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define debug(x) std::cerr<<#x<<": "<<(x)<<'\n'
const int N=200020;
int _, n, k, d, a[N], p[N];
long long f() {
	sort(a+1, a+1+n);
	for(int i=1; i<=n; i++) p[i] = p[i-1]+a[i];
	long long res=1e18;
	for(int i=1; i<=n; i++) {
		d=min(k, i-1);
		long long cur = d*a[i]-p[d];
		if (k>d) {
			d=k-d-1;
			cur += (p[n]-p[n-d]) - d*a[i];
		}
		res = min(res, cur);
	}
	return res;
}
int main() {
	scanf("%d%d", &n, &k);
	for(int i=1; i<=n; i++) scanf("%d", a+i);
	long long ans=f();
	for(int i=1; i<=n; i++) a[i]=-a[i];
	ans = min(ans, f());
	printf("%lld\n", ans);
}


#include <bits/stdc++.h>
using namespace std;
#define debug(x) std::cerr<<#x<<": "<<(x)<<'\n'
const int N=200020;
long long p[N];
int _, n, k, a[N];
long long f() {
	sort(a+1, a+1+n);
	for(int i=1; i<=n; i++) p[i] = p[i-1]+a[i];
	long long res=1e18;
	for(int i=1; i<=n; i++) {
		int d=min(k, i-1);
		long long cur = d*1ll*a[i]-p[d];
		if (k>d) {
			d=k-d-1;
			cur += (p[n]-p[n-d]) - d*1ll*a[i];
		}
		res = min(res, cur);
	}
	return res;
}
int main() {
	scanf("%d%d", &n, &k);
	for(int i=1; i<=n; i++) scanf("%d", a+i);
	long long ans=f();
	for(int i=1; i<=n; i++) a[i]=-a[i];
	ans = min(ans, f());
	printf("%lld\n", ans);
}


#include <bits/stdc++.h>
using namespace std;
#define debug(x) std::cerr<<#x<<": "<<(x)<<'\n'
const int N=200020;
long long p[N];
int _, n, k, a[N];
long long f() {
	sort(a+1, a+1+n);
	for(int i=k; i<n; i++)
		if (a[i]==a[i-(k-1)])
			return 0;
	for(int i=1; i<=n; i++) p[i] = p[i-1]+a[i];
	long long res=1e18;
	for(int i=1; i<=n; i++) {
		int d=min(k, i);
		long long cur = (i-1)*1ll*(a[i]-1)-p[i-1]+d-1;
		if (k>d)
			cur += (p[n]-p[i]) - (n-i)*1ll*(a[i]+1) + k-d;
		res = min(res, cur);
	}
	return res;
}
int main() {
	scanf("%d%d", &n, &k);
	for(int i=1; i<=n; i++) scanf("%d", a+i);
	long long ans=f();
	for(int i=1; i<=n; i++) a[i]=-a[i];
	ans = min(ans, f());
	printf("%lld\n", ans);
}


#include <bits/stdc++.h>
using namespace std;
#define debug(x) std::cerr<<#x<<": "<<(x)<<'\n'
const int N=200020;
long long p[N];
int _, n, k, a[N];
long long f() {
	sort(a+1, a+1+n);
	for(int i=k; i<n; i++)
		if (a[i]==a[i-(k-1)])
			return 0;
	for(int i=1; i<=n; i++) p[i] = p[i-1]+a[i];
	long long res=1e18;
	for(int i=1; i<=n; i++) {
		int d=min(k, i-1);
		long long cur = d*1ll*a[i]-p[d];
		if (k>d+1) {
			d=k-d-1;
			cur += (p[n]-p[i]) - (n-i)*1ll*(a[i]+(a[i]>0?1:-1)) + d;
		}
		res = min(res, cur);
	}
	return res;
}
int main() {
	scanf("%d%d", &n, &k);
	for(int i=1; i<=n; i++) scanf("%d", a+i);
	long long ans=f();
	for(int i=1; i<=n; i++) a[i]=-a[i];
	ans = min(ans, f());
	printf("%lld\n", ans);
}


#include <bits/stdc++.h>
using namespace std;
#define debug(x) std::cerr<<#x<<": "<<(x)<<'\n'
const int N=200020;
long long p[N];
int _, n, k, a[N];
long long f() {
	sort(a+1, a+1+n);
	for(int i=1; i<=n; i++) p[i] = p[i-1]+a[i];
	long long res=1e18;
	for(int i=1; i<=n; i++) {
		int d=min(k, i-1);
		long long cur = d*1ll*a[i]-p[d];
		if (k>d+1) {
			d=k-d-1;
			cur += (p[n]-p[i]) - (n-i)*1ll*(a[i]+(a[i]>0?1:-1)) + d;
		}
		res = min(res, cur);
	}
	return res;
}
int main() {
	scanf("%d%d", &n, &k);
	for(int i=1; i<=n; i++) scanf("%d", a+i);
	long long ans=f();
	for(int i=1; i<=n; i++) a[i]=-a[i];
	ans = min(ans, f());
	printf("%lld\n", k==1?0:ans);
}


#include <bits/stdc++.h>
using namespace std;
#define debug(x) std::cerr<<#x<<": "<<(x)<<'\n'
const int N=200020;
long long p[N];
int _, n, k, a[N];
long long f() {
	sort(a+1, a+1+n);
	for(int i=k; i<n; i++)
		if (a[i]==a[i-(k-1)])
			return 0;
	for(int i=1; i<=n; i++) p[i] = p[i-1]+a[i];
	long long res=1e18;
	for(int i=1; i<=n; i++) {
		int d=min(k, i-1);
		long long cur = d*1ll*a[i]-p[d];
		if (k>d+1)
			cur += (p[n]-p[i]) - (n-i)*1ll*(a[i]+1) + k-d-1;
		res = min(res, cur);
	}
	return res;
}
int main() {
	scanf("%d%d", &n, &k);
	for(int i=1; i<=n; i++) scanf("%d", a+i);
	long long ans=f();
	for(int i=1; i<=n; i++) a[i]=-a[i];
	ans = min(ans, f());
	printf("%lld\n", ans);
}


#include <bits/stdc++.h>
using namespace std;
#define debug(x) std::cerr<<#x<<": "<<(x)<<'\n'
const int N=200020;
long long p[N];
int _, n, k, a[N];
long long f() {
	sort(a+1, a+1+n);
	for(int i=1; i<=n; i++) p[i] = p[i-1]+a[i];
	long long res=1e18;
	for(int i=1; i<=n; i++) {
		int d=min(k, i-1);
		long long cur = d*1ll*a[i]-p[d];
		if (k>d) {
			d=k-d-1;
			cur += (p[n]-p[i]) - (n-i)*1ll*(a[i]+(a[i]>0?1:-1)) + d;
		}
		res = min(res, cur);
	}
	return res;
}
int main() {
	scanf("%d%d", &n, &k);
	for(int i=1; i<=n; i++) scanf("%d", a+i);
	long long ans=f();
	for(int i=1; i<=n; i++) a[i]=-a[i];
	ans = min(ans, f());
	printf("%lld\n", k==1?0:ans);
}


#include <bits/stdc++.h>
using namespace std;
#define debug(x) std::cerr<<#x<<": "<<(x)<<'\n'
const int N=200020;
long long p[N];
int _, n, k, a[N];
long long f() {
	sort(a+1, a+1+n);
	for(int i=1; i<=n; i++) p[i] = p[i-1]+a[i];
	long long res=1e18;
	for(int i=1; i<=n; i++) {
		int d=min(k, i-1);
		long long cur = d*a[i]-p[d];
		if (k>d) {
			d=k-d-1;
			cur += (p[n]-p[n-d]) - d*a[i];
		}
		res = min(res, cur);
	}
	return res;
}
int main() {
	scanf("%d%d", &n, &k);
	for(int i=1; i<=n; i++) scanf("%d", a+i);
	long long ans=f();
	for(int i=1; i<=n; i++) a[i]=-a[i];
	ans = min(ans, f());
	printf("%lld\n", ans);
}


#include <bits/stdc++.h>
using namespace std;
#define debug(x) std::cerr<<#x<<": "<<(x)<<'\n'
const int N=200020;
long long p[N];
int _, n, k, a[N];
long long f() {
	sort(a+1, a+1+n);
	for(int i=1; i<=n; i++) p[i] = p[i-1]+a[i];
	long long res=1e18;
	for(int i=1; i<=n; i++) {
		int d=min(k, i-1);
		long long cur = d*1ll*a[i]-p[d];
		if (k>d) {
			d=k-d-1;
			cur += (p[n]-p[i]) - (n-i)*1ll*(a[i]+(a[i]>0?1:-1)) + d;
		}
		res = min(res, cur);
	}
	return res;
}
int main() {
	scanf("%d%d", &n, &k);
	for(int i=1; i<=n; i++) scanf("%d", a+i);
	long long ans=f();
	for(int i=1; i<=n; i++) a[i]=-a[i];
	ans = min(ans, f());
	printf("%lld\n", ans);
}


#include <bits/stdc++.h>
using namespace std;
#define debug(x) std::cerr<<#x<<": "<<(x)<<'\n'
const int N=200020;
long long p[N];
int _, n, k, a[N];
long long f() {
	sort(a+1, a+1+n);
	for(int i=k; i<n; i++)
		if (a[i]==a[i-(k-1)])
			return 0;
	for(int i=1; i<=n; i++) p[i] = p[i-1]+a[i];
	long long res=1e18;
	for(int i=1; i<=n; i++) {
		int d=min(k, i);
		long long cur = d*1ll*a[i]-p[d];
		if (k>d)
			cur += (p[n]-p[i]) - (n-i)*1ll*(a[i]+1) + k-d;
		res = min(res, cur);
	}
	return res;
}
int main() {
	scanf("%d%d", &n, &k);
	for(int i=1; i<=n; i++) scanf("%d", a+i);
	long long ans=f();
	for(int i=1; i<=n; i++) a[i]=-a[i];
	ans = min(ans, f());
	printf("%lld\n", ans);
}


#include <bits/stdc++.h>
using namespace std;
#define debug(x) std::cerr<<#x<<": "<<(x)<<'\n'
const int N=200020;
long long p[N];
int _, n, k, a[N];
long long f() {
	sort(a+1, a+1+n);
	for(int i=k; i<n; i++)
		if (a[i]==a[i-(k-1)])
			return 0;
	for(int i=1; i<=n; i++) p[i] = p[i-1]+a[i];
	long long res=1e18;
	for(int i=1; i<=n; i++) {
		int d=min(k-1, i-1);
		long long cur = d*1ll*a[i]-p[d];
		if (k>d+1)
			cur += (p[n]-p[i]) - (n-i)*1ll*(a[i]+1) + k-d-1;
		res = min(res, cur);
	}
	return res;
}
int main() {
	scanf("%d%d", &n, &k);
	for(int i=1; i<=n; i++) scanf("%d", a+i);
	long long ans=f();
	for(int i=1; i<=n; i++) a[i]=-a[i];
	ans = min(ans, f());
	printf("%lld\n", ans);
}


#include <bits/stdc++.h>
using namespace std;
#define debug(x) std::cerr<<#x<<": "<<(x)<<'\n'
const int N=200020;
long long p[N];
int _, n, k, a[N];
long long f() {
	sort(a+1, a+1+n);
	for(int i=k; i<n; i++)
		if (a[i]==a[i-(k-1)])
			return 0;
	for(int i=1; i<=n; i++) p[i] = p[i-1]+a[i];
	long long res=1e18;
	for(int i=1; i<=n; i++) {
		int d=min(k-1, i);
		long long cur = d*1ll*a[i]-p[d];
		if (k>d)
			cur += (p[n]-p[i]) - (n-i)*1ll*(a[i]+1) + k-d;
		res = min(res, cur);
	}
	return res;
}
int main() {
	scanf("%d%d", &n, &k);
	for(int i=1; i<=n; i++) scanf("%d", a+i);
	long long ans=f();
	for(int i=1; i<=n; i++) a[i]=-a[i];
	ans = min(ans, f());
	printf("%lld\n", ans);
}


#include <bits/stdc++.h>
using namespace std;
#define debug(x) std::cerr<<#x<<": "<<(x)<<'\n'
const int N=200020;
long long p[N];
int _, n, k, a[N];
long long f() {
	sort(a+1, a+1+n);
	for(int i=k; i<n; i++)
		if (a[i]==a[i-(k-1)])
			return 0;
	for(int i=1; i<=n; i++) p[i] = p[i-1]+a[i];
	long long res=1e18;
	for(int i=1; i<=n; i++) {
		int d=min(k, i-1);
		long long cur = d*1ll*a[i]-p[d];
		if (k>d+1) {
			d=k-d-1;
			cur += (p[n]-p[i]) - (n-i)*1ll*(a[i]+(a[i]>0?1:-1)) + (a[i]>0?d:-d);
		}
		res = min(res, cur);
	}
	return res;
}
int main() {
	scanf("%d%d", &n, &k);
	for(int i=1; i<=n; i++) scanf("%d", a+i);
	long long ans=f();
	for(int i=1; i<=n; i++) a[i]=-a[i];
	ans = min(ans, f());
	printf("%lld\n", ans);
}