Berland Football Cup starts really soon! Commentators from all over the world come to the event.Organizers have already built $$$n$$$ commentary boxes. $$$m$$$ regional delegations will come to the Cup. Every delegation should get the same number of the commentary boxes. If any box is left unoccupied then the delegations will be upset. So each box should be occupied by exactly one delegation.If $$$n$$$ is not divisible by $$$m$$$, it is impossible to distribute the boxes to the delegations at the moment.Organizers can build a new commentary box paying $$$a$$$ burles and demolish a commentary box paying $$$b$$$ burles. They can both build and demolish boxes arbitrary number of times (each time paying a corresponding fee). It is allowed to demolish all the existing boxes.What is the minimal amount of burles organizers should pay to satisfy all the delegations (i.e. to make the number of the boxes be divisible by $$$m$$$)?

The only line contains four integer numbers $$$n$$$, $$$m$$$, $$$a$$$ and $$$b$$$ ($$$1 \le n, m \le 10^{12}$$$, $$$1 \le a, b \le 100$$$), where $$$n$$$ is the initial number of the commentary boxes, $$$m$$$ is the number of delegations to come, $$$a$$$ is the fee to build a box and $$$b$$$ is the fee to demolish a box.

Output the minimal amount of burles organizers should pay to satisfy all the delegations (i.e. to make the number of the boxes be divisible by $$$m$$$). It is allowed that the final number of the boxes is equal to $$$0$$$.

NoteIn the first example organizers can build $$$5$$$ boxes to make the total of $$$14$$$ paying $$$3$$$ burles for the each of them.In the second example organizers can demolish $$$2$$$ boxes to make the total of $$$0$$$ paying $$$7$$$ burles for the each of them.In the third example organizers are already able to distribute all the boxes equally among the delegations, each one get $$$5$$$ boxes.

#include <stdio.h>

long long nzv(long long n, long long m) {
	long long x = n / m;
	if(n == m * x) return m * x;
	return m * (x+1);
}

int main() {
	long long n, m, a, b;
	scanf("%lld%lld%lld%lld", &n, &m, &a, &b);
	long long demolish = n % m * b;
	long long build = (nzv(n, m) - n) * a;

	if(build < demolish) printf("%lld", build);
	else printf("%lld", demolish);
	return 0;
}

#include<stdio.h>
int main()
{
    long long int n,m,a,b,i,k,x,y;
    scanf("%lld %lld %lld %lld",&n,&m,&a,&b);
    if(n%m==0)
    {
        printf("0\n");
    }
    else
    {
        k=n%m;
        x=k*b;
        y=(m-k)*a;
        if(x<=y)
        {
            printf("%lld\n",x);
        }
        else
        {
            printf("%lld\n",y);
        }
    }
    return 0;
}

#include<stdio.h>
int main()
{
    long long int n, m, a, b, x, y;
    scanf("%lld%lld%lld%lld", &n, &m, &a, &b);
    if(n%m==0)
    {
        printf("0\n");
    }
    else
    {
        x= (n%m)*b;
        y= (m-(n%m))*a;
        if(x<y)
        {
            printf("%lld", x);
        }
        else
            printf("%lld", y);
    }
    return 0;
}

#include<stdio.h>

int main()
{
  long long int n,m,a,b,acost,bcost,t;
  scanf("%lld%lld%lld%lld",&n,&m,&a,&b);
  if(n%m==0)
   return printf("0\n"),0;
  else
  {
      t=n%m;
      acost=(m-t)*a;
      bcost=t*b;

      if(bcost<acost)
        return printf("%lld\n",bcost),0;
  }
    printf("%lld\n",acost);
}

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

long long int min(long long int a,long long int b)
{
	if(a>b)
		return b;
	return a;
}

int main()
{
	long long int n,m,a,b,ans=0,p,q;
	scanf("%lld%lld%lld%lld",&n,&m,&a,&b);
	p = n%m;
	q = m-p;
	ans = min(p*b,q*a);
	printf("%lld\n",ans );
	return 0;
}

#include<stdio.h>
#include<math.h>

int main()
{

    long long int m,n,a,b,p,q;
    scanf("%I64d %I64d %I64d %I64d",&n,&m,&a,&b);
    if( n%m==0){
        printf("0");
    }
    else if(n>m && n%m!=0){
        p=(n%m)*b;
        q=(m*(ceil(n/(m*1.0)))-n)*a;
        if(p>q){
            printf("%I64d",q);
        }
        else
            printf("%I64d",p);
    }
    else if(n<m){
        p=(m-n)*a;
        q=(n)*b;
        if(p>q){
            printf("%I64d",q);
        }
        else
            printf("%I64d",p);
    }

}

#include<stdio.h>
long long int min (long long int a, long long int b)
{
    if(a<b)
    return a;
    else
    return b;
}
int main()
{
    long long int n,m,a,b;
    scanf("%lld %lld %lld %lld",&n,&m,&a,&b);
    if(n%m==0)
    printf("0");
    else
    {
        long long int rem=n%m;
        long long int des=rem*b;
        long long int build = (((n/m + 1)*m)-n)*a;
        printf("%lld",min(des,build));
    }

}

#include <stdio.h>
int main(){
    long long m,n;
    int a,b;
    scanf("%lld %lld %d %d",&m,&n,&a,&b);
    // tim 2 so gan n nhat
    if (m % n != 0){
        long long f1,f2;
        // find f1
        long long temp1 = m, temp2 = m;
        f1 = temp1/n* n;
        // find f2
        f2 = f1 + n;
       // printf("(%lld %lld)",f1,f2);
        if ( (m-f1)*b < (f2-m)*a )
        printf("%lld",(m-f1)*b);
        else printf("%lld",(f2-m)*a);

    }else printf("0");
    return 0;
}

#include<stdio.h>

int main(void)
{
	long long n, m, a, b;
	long long min;

	scanf("%I64d %I64d %I64d %I64d", &n, &m, &a, &b);

	min = (n % m) * b < (m - (n % m)) * a ? (n % m) * b : (m - (n % m)) * a;

	printf("%I64d", min);
}

#include<stdio.h>

int main()
{
	long long n,m;
	int a,b;
	long long anc,sum;
	int i;
	scanf("%I64d%I64d%d%d",&n,&m,&a,&b);

		anc=sum=0;
	      	if(n%m==0 )
		{
			printf("0\n");

		}
		else
		{

			anc=b*(n%m);

			sum=(m-n%m)*a;

				if(anc<sum)
				{
					printf("%I64d\n",anc);

				}
				else
				{
					printf("%I64d\n",sum);

				}

		return 0;

	}
	return 0;
}

/* Coached by rainboy */
#include <stdio.h>

long long min(long long a, long long b) {
	return a < b ? a : b;
}

int main() {
	long long n, m;
	int a, b;

	scanf("%lld%lld%d%d", &n, &m, &a, &b);
	printf("%lld\n", n % m == 0 ? 0 : min(n % m * b, (m - n % m) * a));
	return 0;
}

#include<stdio.h>
main()
{
    long long int n,m;int a,b;
    scanf("%I64d%I64d%d%d",&n,&m,&a,&b);
    if(n%m!=0)
    {
        if((m-n%m)*a<(n%m)*b)printf("%I64d",(m-n%m)*a);
        else printf("%I64d",(n%m)*b);
        return 0;

    }
    printf("0");
    return 0;
}

#include<stdio.h>
int main()
{
    long long int n,m,a,b,ans1,ans2,i,p;
    scanf("%I64d%I64d%I64d%I64d",&n,&m,&a,&b);
    if(n%m==0)
    {

        printf("");
    }
    if(n<m)
    {
        ans1=((m-n)*a);
        ans2=n*b;
    }
    else
    {
        ans1=(n%m)*b;
        ans2=((((n/m)+1)*m)-n)*a;
    }

    if(ans1<ans2)
        printf("%I64d",ans1);
    else
        printf("%I64d",ans2);

    return 0;

}

//Bismillahir rahmanir rahim

#include <stdio.h>

int main(void) {
	// your code goes here
	long long  m,n,a,b,rem,ans;
	scanf("%lld %lld %lld %lld",&m,&n,&a,&b);
	rem = m % n ;
	long long int ans1 = rem*b;
	long long int ans2 = (n-rem)*a;
	if(ans1>ans2)   ans=ans2;
	else ans=ans1;
	printf("%lld",ans);
	return 0;
}

main()
{long long int i,j,k,p,l,n,m,a,b;
scanf("%lld%lld%lld%lld",&n,&m,&a,&b);
k=n%m;
j=m-k;
i=j*a;
p=b*k;
//printf("%d %d %d %d\n",k,j,i,p);
if (i>p)printf("%lld",p);
else if (p>=i) {printf("%lld",i);}
}

#include<stdio.h>
int main(){
  long long int n,m;
  int a,b;
  scanf("%lld%lld%d%d",&n,&m,&a,&b);
  if(n%m==0)
  printf("0\n");
  else if(n%m!=0)
  {
    long long int temp1=n/m;
    long long int temp2=temp1+1;
    long long int ans1=(n-(temp1*m))*b;
    long long int ans2=((temp2*m)-n)*a;
    if(ans1<ans2)
    printf("%lld\n",ans1);
    else
    printf("%lld\n",ans2);
  }
}

#include<stdio.h>
int main()
{
    long long n,m,a,b,ans,c1,c2,yu;
    scanf("%I64d %I64d %I64d %I64d",&n,&m,&a,&b);
    yu=n%m;
    if(yu==0)
        printf("0\n");
    else
    {
        c1=yu*b;
        c2=(m-yu)*a;
        if(c1>c2)
            printf("%I64d\n",c2);
        else
            printf("%I64d\n",c1);
    }
    return 0;
}

#include<stdio.h>
#define ll long long
#include<math.h>

int main()
{
	ll n,m,a,b;

	scanf("%lld%lld%lld%lld",&n,&m,&a,&b);

	ll r1,r2,c1,c2;

	if(n%m==0)
	{
		printf("0\n");
	}

	else if(n>m)
	{
		r1=n%m;
		r2=m-r1;

	        	c1=r2*a;
	        	c2=r1*b;

		       if(c1<c2)
		       printf("%lld\n",c1);

		       else
		       printf("%lld\n",c2);
	}

	else if(m>n)
	{
		r1=m-n;
		r2=n;

		c1=r1*a;
		c2=r2*b;

		        if(c1<c2)
		       printf("%lld\n",c1);

		       else
		       printf("%lld\n",c2);
	}
	return 0;

}

#include <stdio.h>

int main()
{
	long long m,n ;
	int a,b ;
	scanf("%lld%lld%d%d",&m,&n,&a,&b) ;
	int i ;
	long long cost1,cost2 ;
	if(m==n || m%n==0)
		cost1 = cost2 = 0 ;
	else if(m<n)
	{
		cost1 = (n-m)*a ;
		cost2 = m*b ;
	}
	else
	{
		long long t = m/n ;
		cost1 = a*((t+1)*n-m) ;
		cost2 = b*(m-t*n) ;
	}
	printf("%lld\n",cost1>=cost2?cost2:cost1) ;
	return 0 ;
}

#include <stdio.h>
#define ll long long
int main(){
	ll n, m;
	int a, b;
	scanf("%I64d%I64d%d%d",&n,&m,&a,&b);
	if (n%m == 0) printf("0");
	else{
		ll costa = a*(m-n%m);
		ll costb = b*(n%m);
		printf("%I64d",costa<costb? costa:costb);
	}

	return 0;
}

#include <stdio.h>
#include <inttypes.h>

long long int mod(long long int x, long long int y){
	return(x-(y*(x/y)));
}

int main(void){

	long long int n, m, a, b;
	scanf("%I64d%I64d%I64d%I64d", &n, &m, &a, &b);

	long long int division = mod(n, m);

	if(division!=0){
		if((m-division)*a < division*b){
			printf("%I64d\n", (m-division)*a);
		}else{
			printf("%I64d\n", division*b);
		}
	}else{
		printf("0\n");
	}

	return 0;
}

#include<stdlib.h>
#include<stdio.h>

int main()
{
short int a=0,b=0;
unsigned long long int n=0,m=0,modnm=0;
scanf("%llu %llu %hd %hd",&n,&m,&a,&b);
modnm=n%m;
if (b*modnm>a*(m-modnm))
{
printf("%llu",a*(m-modnm));
}else
{
printf("%llu",b*modnm);
}
return 0;
}

#include<stdio.h>
#include<stdlib.h>

int main()
{
	long long int n, m, a, b;
	scanf("%lld%lld%lld%lld", &n, &m, &a, &b);
	long long int mod = n%m;
	if(mod==0)
	{
		printf("0\n");
	}
	else
	{
		long long int dem = b*mod;
		long long int cons = a*(m-mod);
		if(dem<cons)
		{
			printf("%lld\n", dem);
		}
		else
		{
			printf("%lld\n", cons);
		}
	}
	return 0;
}

#include <stdio.h>

int main(){

	long long n,m,a,b;
	scanf("%lld %lld %lld %lld",&n,&m,&a,&b);
	printf("%lld\n",((n%m)*b<((m-(n%m))%m)*a)?(n%m)*b:((m-(n%m))%m)*a);

	return 0;
}

#include <stdio.h>
long long min(long long a,long long b) {return (a<b)?a:b;}
int main()
{
    long long n,m;
    int a,b;
    scanf("%lld%lld%d%d",&n,&m,&a,&b);
    long long r=n%m;
    printf("%lld\n",min(r*b,(m-r)*a));
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    long long n, m, a, b;
    scanf("%I64d%I64d%I64d%I64d", &n, &m, &a, &b);

    if((n%m)*b<(m-n%m)*a) printf("%I64d\n", (n%m)*b);
    else printf("%I64d\n", (m-n%m)*a);

    return 0;
}

#include<stdio.h>
int main()
{
    long long int n,m,a,b,i,j;
    scanf("%lld%lld%lld%lld",&n,&m,&a,&b);
    if(n%m!=0)
    {
       if(n<m)
       {
           i=n*b;
           j=(m-n)*a;
           if(i<j)
           {
               printf("%lld\n",i);
           }
           else
           {
               printf("%lld\n",j);
           }
       }
       else if(n>m)
       {
           i=(n%m)*b;
           j=(m-(n%m))*a;
           if(i<j)
           {
               printf("%lld\n",i);
           }
           else
           {
               printf("%lld\n",j);
           }

       }
    }
    else
    {
        i=0;
        printf("%lld\n",i);
    }
    return 0;
}

#include<stdio.h>
int main(){
    long long int n,m,d;
    int a,b,i;
    scanf("%lld %lld %d %d",&n,&m,&a,&b);
    long long int sum_a=0, sum_b=0;

    if(n%m==0)
        printf("0");
    else{
        d=n%m;
        sum_a= (m-d)*a;
        sum_b= d*b;
        if(sum_a<sum_b)
            printf("%lld",sum_a);
        else
            printf("%lld",sum_b);
    }
}

#include <stdio.h>

int main()
{
    long long int n, m, a, b, x, y, c1, c2, div;
    scanf("%lli %lli %lli %lli", &n, &m, &a, &b);

    if(n%m==0)
    {
        printf("0\n");
    }
    else
    {
        div=n/m;
        x=n-(div*m);
        c2=b*x;
        //printf("div %lli\n", div);
        //printf("c2 %lli\n", c2);

        div++;
        y=(div*m)-n;
        c1=a*y;
        //printf("c1 %lli\n", c1);

        if(c1>c2)
        {
            printf("%lli\n", c2);
        }
        else
        {
            printf("%lli\n", c1);
        }
    }
}

#include<stdio.h>
int main()
{
	long long n,m,a,b,c,d;
	while(scanf("%I64d%I64d%I64d%I64d",&m,&n,&a,&b)!=EOF)
	{
		if(m%n==0)
		{
			printf("0");
			printf("\n");
		}
		else
		{
			c=m%n*b;//拆除
				if(m>n)
				{
					d=(n-m%n)*a;
					if(c>d)
					{
						printf("%I64d\n",d);
					}
					else
					{
						printf("%I64d\n",c);
					}

				 }
				 else//m<n
				 {
				 	d=(n-m)*a;//重建
				 	if(c>d)
					{
						printf("%I64d\n",d);
					}
					else
					{
						printf("%I64d\n",c);
					}
				 }

		}
	}
	return 0;
}

#include<stdio.h>
main()
{
   long long int n,m,a,b,p,q,cntd=0,cntb=0,i,j,k,d=0,b1=0,g=0,e=0;
    scanf("%lld%lld%lld%lld",&n,&m,&a,&b);
    p=n;q=n;
    cntd=p%m;
    d=cntd*b;
    if(q%m==0)cntb=0;
    else
    {
        q=q+m;
        g=q/m;
        cntb=g*m-n;
    }
    b1=cntb*a;
    e=n*b;
  if(e<d && e<b1)printf("%lld\n",e);
   else
   {  if(b1<d)printf("%lld\n",b1);

    else printf("%lld\n",d);

    }

    return 0;

}

#include<stdio.h>
int main()
{
	long long n, m, a, b ;
	scanf("%I64d %I64d %I64d %I64d", &n, &m, &a, &b);
	if (n >= m) {
		long long x, y;
		x = n % m;
		y = n / m;
		if (b*x >= ((y + 1)* m-n)*a) printf("%I64d", ((y + 1)*m-n)*a);
		else printf("%I64d", b*x);
	}
	else if (n < m) {
		if (b*n >= (m - n)*a) printf("%I64d", (m-n)*a);
		else printf("%I64d", b*n);
	}
}

#include<stdio.h>
long long int n,m,y,r,s;
int a,b;
int main()
{
    scanf("%lld %lld %d %d",&n,&m,&a,&b);
    if(n%m==0)
     s=0;
    else
     {
        r=a*(m-(n%m));
        y=b*(n%m);
        if(r>y)
        s=y;
        else
        s=r;
     }
 printf("%lld",s);
}

#include<stdio.h>
int main()
{
    long long int n,m,rem=0,cost1=0,cost2=0;
    int a,b;
    scanf("%lld %lld %d %d",&n,&m,&a,&b);
    rem=n%m;
    if(rem==0)
    {
        printf("0");
        return 0;
    }
    if((rem>0)&&(n>m))
    {
        cost1=rem*b;
        cost2=(m-rem)*a;
        cost1<cost2?printf("%lld",cost1):printf("%lld",cost2);
        return 0;
    }
    if((rem>0)&&(m>n))
    {
        cost1=n*b;
        cost2=(m-n)*a;
        cost1<cost2?printf("%lld",cost1):printf("%lld",cost2);
        return 0;
    }
}

#include <stdio.h>
#include <stdlib.h>

int main()
{
    long long  n,m,a,b,x1,x2;
    scanf("%lld",&n);
    scanf("%lld",&m);
    scanf("%lld",&a);
    scanf("%lld",&b);
    x1=n%m;
    x2=m-x1;
    if(a*x2 > x1*b)
        printf("%lld",x1*b);
    else
        printf("%lld",x2*a);
    return 0;
}

/* Coached by rainboy */
#include <stdio.h>

long long min(long long a, long long b) {
	return a < b ? a : b;
}

int main() {
	long long n, m;
	int a, b;

	scanf("%lld%lld%d%d", &n, &m, &a, &b);
	printf("%lld\n", n % m == 0 ? 0 : min(n % m * b, (m - n % m) * a));
	return 0;
}

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define ll long long
int main(void)
{
    ll n,m,a,b;
    ll c1,c2,t;
    scanf("%lld%lld%lld%lld",&n,&m,&a,&b);

    if(n%m==0) printf("0");
    else
    {
        if(n>m)
        {
            c1=(n%m)*b;
            c2=(ceil(n*1./m)*m-n)*a;
            t=(c1>=c2)?c2:c1;
            printf("%lld",t);
        }
        else
        {
            c1=(m-n)*a;
            c2=n*b;
            t=(c1>=c2)?c2:c1;
            printf("%lld",t);
        }
    }

    return 0;
}

#include<stdio.h>
int main()
{
    long long int n,m,a,b,p,q;
    scanf("%lld %lld %lld %lld",&n,&m,&a,&b);
    if(n%m==0)
    {
        printf("0");
    }
    else
    {
        p=n%m;
        q=m-p;
        if((p*b)>=(q*a))
        {
            printf("%lld",(q*a));
        }
        else
        {
            printf("%lld",(p*b));
        }
    }
}

#include <stdio.h>
int main()
{
    long long int n, m, a, b, c, d;
    scanf("%I64d %I64d %I64d %I64d", &n, &m, &a, &b);
    c=n%m;
    d=m-c;
    if(d*a>c*b)
        printf("%I64d\n", c*b);
    else
        printf("%I64d\n", d*a);

    return 0;
}

#include<stdio.h>
main()
{
    long long int n,m;int a,b;
    scanf("%I64d%I64d%d%d",&n,&m,&a,&b);
    if(n%m!=0)
    {
        if((m-n%m)*a<(n%m)*b)printf("%I64d",(m-n%m)*a);
        else printf("%I64d",(n%m)*b);
        return 0;

    }
    printf("0");
    return 0;
}

#include <stdio.h>

long long min(long long a, long long b)
{
    return a < b? a: b;
}

int main()
{
    long long n, m, a, b;
    scanf("%lld%lld%lld%lld", &n, &m, &a, &b);
    long long demolish = n % m, build = m - demolish;
    printf("%lld\n", min(demolish*b, build*a));
    return 0;
}

#include<stdio.h>
int main()
{
long long int n,m,a,b,minimum_burles,bulit_boxes,destroyed_boxes;
scanf("%I64d %I64d %I64d %I64d",&n,&m,&a,&b);
bulit_boxes=(m-n%m)*a;
destroyed_boxes=(n%m)*b;
if(bulit_boxes<destroyed_boxes) minimum_burles=bulit_boxes;
else minimum_burles=destroyed_boxes;
printf("%I64d",minimum_burles);
return 0;
}

#include<stdio.h>
int main(){
    long long int n,m,a,b;
    scanf("%I64d%I64d%I64d%I64d",&n,&m,&a,&b);
    if(n%m==0) printf("0");
    else{
        long long int d;
        d=n%m;
        if(d*b<((((n/m)+1)*m)-n)*a)
            printf("%I64d",d*b);
        else
            printf("%I64d",((((n/m)+1)*m)-n)*a);
    }
}

#include <stdio.h>
#include <string.h>
int main()
{
    long long int n,m,a,b,c,d;
    scanf("%lld%lld%lld%lld",&n,&m,&a,&b);
    if(n>m)
    {
        if(n%m==0)
        printf("0");
        else
        {
        		c=a*(m-(n%m));
        		d=b*(n%m);
        		if(c<d)
        			printf("%lld",c);
        		else
        			printf("%lld",d);
        }
    }
    else
    {
        c=((m-n)*a);
        d=(n*b);
        if(c<d)
        printf("%lld",c);
        else
        printf("%lld",d);
    }
    return 0;
}

#include<stdio.h>
int main()
{
    long long int i, j, a, b, n, m, sum = 0, sum1 = 0;
    scanf("%lld %lld %lld %lld", &n, &m, &a, &b);
    if(n % m != 0)
    {
        if(n < m)
            {
                sum = n*b;
                sum1 = (m - n)*a;
                if(sum1 > sum)
                    printf("%lld\n", sum);
                else
                    printf("%lld\n", sum1);
            }
        else
            {
                sum = a*(m - n%m);
                sum1 = b*(n%m);
                if(sum1 > sum)
                    printf("%lld\n", sum);
                else
                    printf("%lld\n", sum1);
            }

    }
    else
        printf("%lld\n", sum);

}

#include<stdio.h>
int main(){
    int a,b;
    long long n,m;
    long long v1,v2;
    scanf("%lld%lld%d%d",&n,&m,&a,&b);
    v1=n%m;
    v2=m-v1;
    printf("%lld",(v2*a<v1*b)?(v2*a):(v1*b));
    return 0;
}

#include<stdio.h>
#define I64 long long int
int main()
{
    I64 n,m,a,b,p,q,k,k_;
    scanf("%I64d%I64d%I64d%I64d",&n,&m,&a,&b);
    if(n%m==0){
        printf("0");
        return 0;
    }
    if(n>m)
    {
        p=n%m;q=m-p;
        k=q*a;
        k_=p*b;
        k<k_?printf("%I64d",k):printf("%I64d",k_);
    }
    else
    {
        p=m-n;
        k=p*a;
        k_=n*b;
        k<k_?printf("%I64d",k):printf("%I64d",k_);
    }
}

#include<stdio.h>
int main()
{
    long long unsigned int n,m,a,b,p=0,q=0;
    scanf("%lld%lld%lld%lld",&n,&m,&a,&b);

    p = (n%m)*b;
    q = (m-(n%m))*a;

    if(p>q)
        printf("%lld\n",q);
    else
        printf("%lld\n",p);

    return 0;
}

#include<stdio.h>
#include<stdlib.h>
#define min(x,y) x<y?x:y
int main()
{
    long long m,n,a,b,ccost,dcost;
    scanf("%lld%lld%lld%lld",&n,&m,&a,&b);
    if(n==m) printf("0");
    else if(n>m)
    {
        long long dr=n%m,cr=((n/m)+1)*m-n;
        printf("%lld",min(cr*a,dr*b));
    }
    else
    {
       long long diff=m-n;
       printf("%lld",min(diff*a,n*b));
    }

    return 0;
}

#include<stdio.h>
int main()
{
	long long int n,m;
	scanf("%lld %lld",&n,&m);
	int a,b;
	scanf("%d %d",&a,&b);
	long long int c1=0,c2=0;
	c1 = (m-(n%m)) * a;
	c2 = (n%m)*b;
	if (c1<c2)
		printf("%lld\n",c1);
	else
		printf("%lld\n",c2);
	return 0;
}

#include <stdio.h>
#define ll long long

ll min(ll a, ll b)
{
	return a >= b ? b : a;
}

int main()
{
	ll n, m, a, b, x, ans;
	scanf("%lld %lld %lld %lld", &n, &m, &a, &b);
	x = n / m;
	ans = min(((x + 1) * m - n) * a, (n - x * m) * b);
	printf("%lld\n", ans);
	return 0;
}

#include <stdio.h>

int main() {
  long long a, b,c,d,e,f,g,h,i;
  scanf("%lld%lld%lld%lld",&a,&b,&c,&d);
  if (a% b == 0) {
    printf("0\n");
    return 0;
  } else if (a> b) {
    e = (a/ b+ 1) * b- a;
    f = a % b;
    g=e*c;
    h=f*d;
    if(g<h)
    {
    	printf("%lld\n",g);
    	return 0;
    }
    else
    {
    	printf("%lld\n",h);
    	return 0;
    }
  }
   else {
    e = b - a;
    f= a;
    g=e*c;
    h=f*d;
    if(h<g)
    {
    	printf("%lld\n",h);
    	return 0;
    }
    else
    {
    	printf("%lld\n",g);
    	return 0;
    }

  }
  return 0;
}

#include<stdio.h>
int main()
{
    long long n,m,a,b,ans,c1,c2,yu;
    scanf("%I64d %I64d %I64d %I64d",&n,&m,&a,&b);
    yu=n%m;
    if(yu==0)
        printf("0\n");
    else
    {
        c1=yu*b;
        c2=(m-yu)*a;
        if(c1>c2)
            printf("%I64d\n",c2);
        else
            printf("%I64d\n",c1);
    }
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

long long int min(long long int a,long long int b)
{
	if(a>b)
		return b;
	return a;
}

int main()
{
	long long int n,m,a,b,ans=0,p,q;
	scanf("%lld%lld%lld%lld",&n,&m,&a,&b);
	p = n%m;
	q = m-p;
	ans = min(p*b,q*a);
	printf("%lld\n",ans );
	return 0;
}

#include <stdio.h>

int
main ()
{
	unsigned long long int n, m, a, b;

	scanf ("%I64u %I64u %I64u %I64u", &n, &m, &a, &b);

	unsigned long long int destroy = n % m;
	unsigned long long int build = m - destroy;

	build *= a;
	destroy *= b;

	printf ("%I64u\n", build < destroy ? build : destroy);

	return 0;
}

#include<stdio.h>
#include<math.h>
int main()
{
    double n,m,a,b,c,f,p,q;
    scanf("%lf %lf %lf %lf", &n,&m,&a,&b);
    c=ceil(n/m);
    f=floor(n/m);
    p=(c*m-n)*a;
    q=(n-f*m)*b;
    printf("%.0lf",p>q?q:p);
    return 0;
}

#include<stdio.h>
#include<math.h>
int main(){
    long long int n, m;
    int a, b;
    scanf("%I64d%I64d%d%d", &n, &m, &a, &b);
    if(n % m ==0)
        printf("0");
    else{
        /**Hallar el m�ltiplo m�s cercano a m, por encima y por
        debajo de n:
            dividir n entre m y tomar el resultado, por exceso y por defecto
            multiplicar dichos resultados por m y ya tenemos los m�ltiplos buscados*/
        long long int ex, de;
        ex = n/m + 1;
        de = n/m;
        ex = ex * m;
        de = de * m;
        if(de == 0)
            de = n * b;
        else{
            de = fabs(de - n);
            de = de * b;
        }
        ex = fabs(ex - n);
        ex = ex * a;
        if(ex > de)
            printf("%I64d", de);
        else
            printf("%I64d", ex);
    }
    return 0;
}

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#include<time.h>

int main()
{
    long long int n,m,a,b,r,i,j;
    scanf("%I64d %I64d %I64d %I64d",&n,&m,&a,&b);
    if(n%m==0)
        printf("0");
    else if(n%m!=0)
    {
     r=n%m;
      i=r*b;
      j=(m-r)*a ;
      if(i<j)
        printf("%I64d",i);
      else printf("%I64d",j);
    }
}

#include<stdio.h>
#include<stdlib.h>
long long fun(long long ,long long ,long long ,long long );
long long fun(long long n,long long m,long long a,long long b)
{
    long long t=n%m;
    if(t==0)
    {
        return 0;
    }
    long long s1=b*t;
    long long s2=(m-t)*a;
    return (s1<s2)?s1:s2;
}
int main()
{
    long long n,m,a,b;
    scanf("%lld%lld%lld%lld",&n,&m,&a,&b);
    printf("%lld\n",fun(n,m,a,b));
    return 0;
}

#include <stdio.h>

int main(void) {
	long long n,m,a,b,c,d,e,k,l;
	scanf("%lld%lld%lld%lld",&n,&m,&a,&b);
	c=n%m;
	if(c==0)
	{
	  printf("0");
	}
	else
	{
	  d=m-c;
	  k=d*a;
	  l=c*b;
	  if(k>l)
	  {
	      printf("%lld",l);
	  }
	  else
	  {
	      printf("%lld",k);
	  }
	}
	return 0;
}

#include<stdio.h>
#define min(x,y) x<y?x:y
int main()
{
long long int n,m,a,b,x,y;
scanf("%I64d %I64d %I64d %I64d",&n,&m,&a,&b);
x=(m-(n%m))*a;
y=(n%m)*b;
printf("%I64d",min(x,y));
return 0;
}

#include<stdio.h>
int main()
{
	long long int n,m,a,b,x,y;
	scanf("%lld%lld%lld%lld",&n,&m,&a,&b);
	x=n%m;
	if(x==0)
	printf("0\n");
	else if((m-x)*a>x*b)
		printf("%lld\n",x*b);
	else
		printf("%lld\n",(m-x)*a);
	return 0;
}

#include<stdio.h>
#include<math.h>

int main()
{

        long long int n,m,a,b,result,result2,temp,temp2,nn,mm;

        scanf("%lld%lld%lld%lld",&n,&m,&a,&b);
        temp=n;nn=n;temp2=m,mm=m;
        if(n%m==0)
            {printf("0\n");}
        else
        {
            nn=n%m;
            result2=nn*b;
            result=(m-nn)*a;
            if(result2>result)
                printf("%lld\n",result);
            else
                printf("%lld\n",result2);
        }
}

#include <stdio.h>

int main(){

	long long n,m,a,b;
	scanf("%lld %lld %lld %lld",&n,&m,&a,&b);
	printf("%lld\n",((n%m)*b<((m-(n%m))%m)*a)?(n%m)*b:((m-(n%m))%m)*a);

	return 0;
}

#include<stdio.h>
#define min(a,b) a<b?a:b
int main()
{
    long long n,m,a,b,ta,tb;
    scanf("%lld%lld%lld%lld",&n,&m,&a,&b);
    ta=n%m;
    tb=m-ta;
    ta*=b;
    tb*=a;
    printf("%lld\n",min(ta,tb));
    return 0;
}

#include <stdio.h>
#include <stdlib.h>

int main()
{
    long long int a,b,n,m,x,y;
    scanf("%lld %lld %lld %lld",&a,&b,&n,&m);
    x=a%b;
    y=b-a%b;
    if(a%b==0) printf("0\n");
    else
    {
        if(x*m>=y*n) printf("%lld\n",y*n);
        else printf("%lld\n",m*x);
    }
    return 0;
}

#include <stdio.h>

int main()
{
    long long int a, b, c, n, m, x, y, z, cost1, cost2;

    scanf("%lld %lld %lld %lld", &n , &m, &a, &b);

    if(n % m == 0)
    {
        printf("0\n");
        return 0;
    }
    c = n / m;
    x = ((c+1)*m) - n;
    y = n - (c*m) ;
    //printf("%d\n", y);

    cost1 = x*a;
    cost2 = y*b;

    if(cost1<cost2)
        z = cost1;
    else
        z = cost2;

    printf("%lld\n", z);

    return 0;
}

#include<stdio.h>
int main()
{
    long long int n,m,a,b,ans1,ans2,i,p;
    scanf("%I64d%I64d%I64d%I64d",&n,&m,&a,&b);
    if(n%m==0)
    {

        printf("");
    }
    if(n<m)
    {
        ans1=((m-n)*a);
        ans2=n*b;
    }
    else
    {
        ans1=(n%m)*b;
        ans2=((((n/m)+1)*m)-n)*a;
    }

    if(ans1<ans2)
        printf("%I64d",ans1);
    else
        printf("%I64d",ans2);

    return 0;

}

#include <stdio.h>
#define ll long long
int main(){
	ll n, m;
	int a, b;
	scanf("%I64d%I64d%d%d",&n,&m,&a,&b);
	if (n%m == 0) printf("0");
	else{
		ll costa = a*(m-n%m);
		ll costb = b*(n%m);
		printf("%I64d",costa<costb? costa:costb);
	}

	return 0;
}

#include<stdio.h>
#define ll long long
int main()
{
	ll n,m,a,b,x1,x2,res1,res2;
	scanf("%I64d%I64d%I64d%I64d",&n,&m,&a,&b);
	x1=n%m;
	x2=m-x1;
	res1=x1*b;
	res2=x2*a;
	if(res1<res2)
		printf("%I64d",res1);
	else
		printf("%I64d",res2);
	return 0;
}

#include<stdio.h>
#include<math.h>
#define Min(a,b) a<b ? a : b
int main()
{
    long long int n, m, a, b, x, y;
    scanf("%lld%lld%lld%lld",&n,&m,&a,&b);

    y = n%m;
    x = m-(n%m);
    x *= a;
    y *= b;
    printf("%lld\n",Min(x,y));
    return 0;
}

#include<stdio.h>
int main()
{
    long long int n,m,a,b,build,dem;
    scanf("%lld %lld %lld %lld",&n,&m,&a,&b);
    build = m*a -(n%m)*a;
    dem = (n%m)*b;
    build<=dem? printf("%lld",build):printf("%lld",dem);
    return 0;
}

#include<stdio.h>
int main()
{
	long long n,m,y1,y11,y2,y22,x;
	int a,b;
	scanf("%lld%lld",&n,&m);
	scanf("%d%d",&a,&b);
	if(n%m==0)
	printf("0\n");
	else
	{
		x=n/m;
		y1=n-m*x;
		y11=y1*b;
        y2=m*(x+1)-n;
        y22=y2*a;
        if(y11<y22)
        printf("%lld\n",y11);
        else
        printf("%lld\n",y22);

	}
	return 0;
}

#include<stdio.h>
int main()
{
    long long n,m,s,d,r,t;
    int a,b;
    scanf("%lld %lld %d %d",&n,&m,&a,&b);
    if(n%m==0)
    {
        printf("%d",0);
    }
    else
    {
        r=n%m;
        s=r*b;
        t=(m-r);
        d=t*a;
        if(s<=d)
        {
            printf("%lld",s);
        }
        else
        {
            printf("%lld",d);
        }
    }
    return 0;
}

#include<stdio.h>
main()
{
    long long int n,m;int a,b;
    scanf("%I64d%I64d%d%d",&n,&m,&a,&b);
    if(n%m!=0)
    {
        if((m-n%m)*a<(n%m)*b)printf("%I64d",(m-n%m)*a);
        else printf("%I64d",(n%m)*b);
        return 0;

    }
    printf("0");
    return 0;
}

#include<stdio.h>
int main()
{
    int a,b;
    long long int n,m,p,q,t;
    scanf("%lld %lld",&n,&m);
    scanf("%d %d",&a,&b);
    if(n%m==0)
    {
        printf("0");
    }
    else{
        if(n<m){
            p=b*n;
            q=(m-n)*a;
            if(p<q){
                printf("%lld",p);
            }
            else{
                printf("%lld",q);
            }
        }
        else{
            p=(n%m)*b;
            t=(n/m);
            t=t+1;
            q=((t*m)-n)*a;
            if(p<q)
            {
                printf("%lld",p);
            }
            else{
                printf("%lld",q);
            }
        }
    }
}

#include<stdio.h>
int main()
{
    long long int i, j, a, b, n, m, sum = 0;
    scanf("%lld %lld %lld %lld", &n, &m, &a, &b);
    if(n % m != 0)
    {
        if(n < m)
            sum = n*b;
        else
            sum = a*(2*m - n);
    }
    else
        sum = 0;
    printf("%lld\n", sum);
}

#include<stdio.h>

int main()
{
    long long n,m,a,b,p,q,l;
    scanf("%lld%lld%lld%lld",&n,&m,&a,&b);
    if(m>n)
    {
    printf("%lld",n*b);
    }
    else if(n>m)
    {
    l=n/m;
    p=(n-(m*l))*b;
    q=(((l+1)*m)-n)*a;
    if(p>q)
    printf("%lld",q);
    else
    printf("%lld",p);
    }
    else
    printf("%d",0);
    return 0;
}

#include<stdio.h>
#include<math.h>
#define Min(a,b) a<b ? a : b
int main()
{
    int n, m, a, b, d, x, y;
    scanf("%d%d%d%d",&n,&m,&a,&b);
    if(n%m==0)
    {
        printf("0\n");
        return 0;
    }
    d = (m+n-1)/m;
    x = (d*m - n)*a;
    d = Min(n,abs(n-m));
    y = d*b;
    printf("%d\n",Min(x,y));
    return 0;
}

#include <stdio.h>

int main()
{
	long long m,n ;
	int a,b ;
	scanf("%lld%lld%d%d",&m,&n,&a,&b) ;
	int i ;
	int cost1,cost2 ;
	if(m==n || m%n==0)
		cost1 = cost2 = 0 ;
	else if(m>n)
	{
		cost1 = a*(2*n-m) ;
		cost2 = b*(m-n) ;
	}
	else
	{
		cost1 = (n-m)*a ;
		cost2 = m*b ;
	}
	printf("%d\n",cost1>=cost2?cost2:cost1) ;
	return 0 ;
}

#include<stdio.h>
#include<stdlib.h>
#define min(x,y) x<y?x:y
int main()
{
    int m,n,a,b,ccost,dcost;
    scanf("%d%d%d%d",&n,&m,&a,&b);
    if(n==m) printf("0");
    else if(n>m)
    {
        int dr=n%m,cr=((n/m)+1)*m-n;
        printf("%d",min(cr*a,dr*b));
    }
    else
    {
       int diff=m-n;
       printf("%d",min(diff*a,n*b));
    }

    return 0;
}

#include<stdio.h>
int main()
{
    int n,m,a,b,build,dem;
    scanf("%d %d %d %d",&n,&m,&a,&b);
    build = m*a -(n%m)*a;
    dem = (n%m)*b;
    build<=dem? printf("%d",build):printf("%d",dem);
    return 0;
}

#include<stdio.h>
int main()
{
    long long int i, j, a, b, n, m, sum = 0, sum1 = 0;
    scanf("%lld %lld %lld %lld", &n, &m, &a, &b);
    if(n % m != 0)
    {
        if(n < m)
            {
                sum = n*b;
                sum1 = (m - n)*a;
                if(sum1 > sum)
                    printf("%lld\n", sum);
                else
                    printf("%lld\n", sum1);
            }
        else
            {
                sum = a*(2*m - n);
                sum1 = b*(n-m);
                if(sum1 > sum)
                    printf("%lld\n", sum);
                else
                    printf("%lld\n", sum1);
            }

    }
    else
        printf("%lld\n", sum);

}

#include<stdio.h>
#include<string.h>
int main(){
long long int n,m,a,b,c,d;
scanf("%I64d%I64d%I64d%I64d",&n,&m,&a,&b);
if(n%m==0) printf("0");
else{
    c=n%m;
    d=(n/m+1)*m;
    if((d-n)*a>c*b) printf("%d",c*b);
    else printf("%d",a*(d-n));
}
return 0;
}

#include<stdio.h>
#include<math.h>
int main(){
    long long int n, m;
    int a, b;
    scanf("%ld%ld%d%d", &n, &m, &a, &b);
    if(n % m ==0)
        printf("0");
    else{
        /**Hallar el m�ltiplo m�s cercano a m, por encima y por
        debajo de n:
            dividir n entre m y tomar el resultado, por exceso y por defecto
            multiplicar dichos resultados por m y ya tenemos los m�ltiplos buscados*/
        int ex, de;
        ex = n/m + 1;
        de = n/m;
        ex = ex * m;
        de = de * m;
        if(de == 0)
            de = n * b;
        else{
            de = fabs(de - n);
            de = de * b;
        }
        ex = fabs(ex - n);
        ex = ex * a;
        if(ex > de)
            printf("%d", de);
        else
            printf("%d", ex);
    }
    return 0;
}

#include<stdio.h>

int main(void)
{
	long long n, m, a, b;
	long long min;

	scanf("%%I64d %I64d %I64d %I64d", &n, &m, &a, &b);

	min = (n % m) * b < (m - (n % m)) * a ? (n % m) * b : (m - (n % m)) * a;

	printf("%I64d", min);
}

#include<stdio.h>
int main()
{
    int n,m,a,b,i,j;
    scanf("%d%d%d%d",&n,&m,&a,&b);
    if(n%m!=0)
    {
       if(n<m)
       {
           i=n*b;
           j=(m-n)*a;
           if(i<j)
           {
               printf("%d\n",i);
           }
           else
           {
               printf("%d\n",j);
           }
       }
       else if(n>m)
       {
           i=(n%m)*b;
           j=(m-(n%m))*a;
           if(i<j)
           {
               printf("%d\n",i);
           }
           else
           {
               printf("%d\n",j);
           }

       }
    }
    else
    {
        i=0;
        printf("%d\n",i);
    }
    return 0;
}

#include<stdio.h>
#include<math.h>
int main()
{
	int n,m,a,b,res;
	scanf("%d %d %d %d",&n,&m,&a,&b);
	res=n/m;
	if(n % m == 0)
		printf("0");

	else
	{
		if((n-m*res)*b >(m*(res+1)-n)*a)
			printf("%d",(m*(res+1)-n)*a);
		else
			printf("%d",(n-m*res)*b);
	}
}

#include<stdio.h>
int main()
{
    int i, j, a, b, n, m, sum = 0;
    scanf("%d %d %d %d", &n, &m, &a, &b);
    if(n % m != 0)
    {
        if(n < m)
            sum = n*b;
        else
            sum = a*(2*m - n);
    }
    else
        sum = 0;
    printf("%d\n", sum);
}

#include<stdio.h>
int main()
{
    int n,m,a,b,ans,c1,c2,yu;
    scanf("%d %d %d %d",&n,&m,&a,&b);
    yu=n%m;
    if(yu==0)
        printf("0\n");
    else
    {
        c1=yu*b;
        c2=(m-yu)*a;
        if(c1>c2)
            printf("%d\n",c2);
        else
            printf("%d\n",c1);
    }
    return 0;
}

#include<stdio.h>
main()
{
    long int n,m,a,b,p,q,cntd=0,cntb=0,i,j,k,d=0,b1=0,g=0;
    scanf("%ld%ld%ld%ld",&n,&m,&a,&b);
    p=n;q=n;
    cntd=p%m;
    d=cntd*b;
    if(q%m==0)cntb=0;
    else
    {
        q=q+m;
        g=q/m;
        cntb=g*m-n;
    }
    b1=cntb*a;

    if(b1<d)printf("%ld\n",b1);
    else printf("%ld\n",d);
    return 0;

}

#include<stdio.h>
int main()
{
    int c,a,n,m,b,d,e,f;
    scanf("%d%d%d%d",&n,&m,&a,&b);
    c=n%m;
    d=m-n%m;
    b=b*c;
    a=a*d;
    if(a>b)
        printf("%d\n",b);
    else
        printf("%d\n",a);
    return 0;
}

#include<stdio.h>
int main()
{
    int a,b,c,n,m;
    scanf("%d %d %d %d",&n,&m,&a,&b);
    if(n%m==0){
        printf("0\n");
    }
    else{
        if(n<m){
            printf("%d\n",n*b);
        }
        else if(n>m){
            c=(m*2)-n;
            printf("%d\n",c*a);
        }
    }

}

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define ll long long
int main(void)
{
    ll n,m,a,b;
    ll x,y,z;
    ll cost1,cost2;
    scanf("%lld%lld%lld%lld",&n,&m,&a,&b);
    if(n%m==0) printf("0");
    else
    {
        if(n>m)
            x=ceil(n*1./m);
        else
            x=ceil(m*1./n);
        cost1=(x*m-n)*a;
        cost2=(n*b);
        if(cost1>=cost2)
            printf("%lld",cost2);
        else
            printf("%lld",cost1);
    }

    return 0;
}

#include<stdio.h>
int main()
{
    long long int n,m,rem1,rem2,ans1,ans2;
    int a,b;
    scanf("%lld%lld%ld%ld",&n,&m,&a,&b);
    rem1=n%m;
    ans1=rem1*b;
    rem2=m-rem1;
    ans2=rem2*a;
    if(ans1>=ans2)
    {
        printf("%d",ans2);
    }
    else
    {
        printf("%d",ans1);
    }
    return 0;
}

#include<stdio.h>

int main()
{
	int n,m,a,b;
	scanf("%d %d %d %d",&n,&m,&a,&b);

	if(n%m==0)
		{
			printf("%d\n",0);
			return 0;
		}

	int dem,bui;
	bui=(m-n%m)*a;
	dem=(n%m)*b;

	if(bui<dem)
	     printf("%d\n",bui);

	else
		printf("%d\n",dem);

	return 0;

}

#include<stdio.h>
int main()
{
    long int n,m,a,b,c,d;
    scanf("%d%d%d%d",&n,&m,&a,&b);
    if(n>m)
    {
        if(n%m==0)
        printf("0");
        else
        {
        		c=a*(m-(n%m));
        		d=b*(n%m);
        		if(c<d)
        			printf("%d",c);
        		else
        			printf("%d",d);
        }
    }
    else
    {
        c=((m-n)*a);
        d=(n*b);
        if(c<d)
        printf("%d",c);
        else
        printf("%d",d);
    }
    return 0;
}

#include<stdio.h>
int main()
{
    long long int a,b,c,d,x,y,n,m;
    scanf("%lld %lld %lld %lld",&n,&m,&a,&b);
    if(n%m==0){
        printf("0\n");
    }
    else{
        if(n<m){
            x=n*b;
            y=(m-n)*a;
            if(x>y)
            printf("%lld\n",y);
            else
            printf("%lld\n",x);
        }
        else if(n>m){
            c=((m*2)-n)*a;
            d=(n-m)*b;
            if(c>d)
            printf("%lld\n",d);
            else
            printf("%lld\n",c);
        }
    }

}

#include<stdio.h>
int main()
{
    long long n,m,cost1,cost2;
    int a,b;
    scanf("%I64d %I64d %d %d",&n,&m,&a,&b);
    if(n%m==0)
        printf("0");
    else
    {
        if(n>m){
        cost1=((n/m+1)*m-n)*a;
        cost2=(n-(n/m-1)*m)*b;
        }
        else
        {
            cost1=(m-n)*a;
            cost2=n*b;
        }
    if(cost1<=cost2)
        printf("%I64d",cost1);
    else
        printf("%I64d",cost2);
    }
    return 0;
}

//Bismillahir rahmanir rahim

#include <stdio.h>

int main(void) {
	// your code goes here
	int m,n,a,b,rem,ans;
	scanf("%d %d %d %d",&m,&n,&a,&b);
	rem = m % n ;
	int ans1 = rem*b;
	int ans2 = (n-rem)*a;
	if(ans1>ans2)   ans=ans2;
	else ans=ans1;
	printf("%d",ans);
	return 0;
}

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#include<time.h>

int main()
{
    long long int n,m,a,b,r,i,j;
    scanf("%I64 %I64 %I64 %I64",&n,&m,&a,&b);
    if(n%m==0)
        printf("0");
    else if(n%m!=0)
    {
     r=n%m;
      i=r*b;
      j=(m-r)*a ;
      if(i<j)
        printf("%I64",i);
      else printf("%I64",j);
    }
}

#include<stdio.h>
int main()
{

	int m,n,a,b;
	while(scanf("%d%d%d%d",&m,&n,&a,&b)==4)
	{
		int j,k;
		if(m%n==0)
		printf("0\n");
		else{
			j=((2*n)-m)*a;
			k=(m%n)*b;
			if(j>k)
			j=k;
			printf("%d\n",j);
		}
	}
	return 0;
}

#include<stdio.h>
int main(){
    int n,m,a,b;
    scanf("%d%d%d%d",&n,&m,&a,&b);
    int respuesta = 0;
    int costoA,costoB;
    if( n % m != 0 ){
        costoA = a * ( m - ( n % m ) );
        costoB = b * ( n % m );
        if( costoA < costoB ){
            respuesta = costoA;
        }else{
            respuesta = costoB;
        }
    }
    printf("%d",respuesta);
    return 0;
}

#include<stdio.h>

int main()
{
	long long int m,n,a,b;
	int l,d;

	while(scanf("%l64d%l64d%l64d%l64d",&m,&n,&a,&b)!=EOF)
	{
		if(m%n==0)
		{
			printf("0\n");
		}
		else
		{
			l=(n-(m%n))*a;
			d=(m%n)*b;

			if(l<d)
			{
				printf("%d\n",l);
			}
			else
			{
				printf("%d\n",d);
			}
		}

	}

	return 0;
}

#include<stdio.h>
#include<math.h>
int main()
{
	int n,m;
	long long a,b;
	long long anc,sum;
	int i;
	while(scanf("%d%d%ld%ld",&n,&m,&a,&b)==4)
	{
		anc=sum=0;
	      	if(n%m==0)
		{
			printf("0\n");

		}
		else
		{
			if(n>m)
			{
			anc=b*(n-m);

			sum=(2*m-n)*a;
			}
			else
			if(n<m)
			{
				anc=n*b;
				sum=(m-n)*a;
			}
				if(anc<sum)
				{
					printf("%ld\n",anc);

				}
				else
				{
					printf("%ld\n",sum);

				}

		}

	}
	return 0;
}

#include<stdio.h>
int main(void)
{
	int a,b,m,n,sum1=0,sum2=0,sum=0;
	scanf("%d%d%d%d",&n,&m,&a,&b);
	if(n%m==0)
	{
		printf("0\n");
	}
	if(n%m!=0)
	{
		if(n>m)
		{
			sum1=(m-(n-m))*a;
			sum2=(n-m)*b;
			if(sum1<sum2)
			{
				printf("%d\n",sum1);
			}
			if(sum1==sum2)
			{
				printf("%d\n",sum1);
			}
			if(sum1>sum2)
			{
				printf("%d\n",sum2);
			}
		}
		if(n<m)
		{
			sum1=(m-n)*a;
			sum2=n*b;
			if(sum1<sum2)
			{
				printf("%d\n",sum1);
			}
			if(sum1==sum2)
			{
				printf("%d\n",sum1);
			}
			if(sum1>sum2)
			{
				printf("%d\n",sum2);
			}
		}
	}
}

#include <stdio.h>

int main(void) {
    int n,m,a,b,x,a1,a2;
    scanf("%d %d %d %d",&n,&m,&a,&b);
    if(n%m==0)
    {
        printf("0");
    }
    else
    {
        x=n%m;
        a1=(x)*b;
        a2=(m-x)*a;
        if(a1>a2)
        {
            printf("%d",a2);
        }
        else
        {
            printf("%d",a1);
        }
    }
	return 0;
}

#include<stdio.h>

int main()
{
	long long int m,n;
	int a,b,l,d;

	while(scanf("%lld%lld%d%d",&m,&n,&a,&b)!=EOF)
	{
		if(m%n==0)
		{
			printf("0\n");
		}
		else
		{
			l=(n-m%n)*a;
			d=(m%n)*b;

			if(l<d)
			{
				printf("%lld\n",l);
			}
			else
			{
				printf("%lld\n",d);
			}
		}

	}

	return 0;
}

#include<stdio.h>
int main()
{

    long long int m,n,a,b,p,q;
    scanf("%I64d %I64d %I64d %I64d",&n,&m,&a,&b);
    if( n%m==0){
        printf("0");
    }
    else if(n>m && n%m!=0){
        p=(n-m)*b;
        q=((m*2)-n)*a;
        if(p>q){
            printf("%I64d",q);
        }
        else
            printf("%I64d",p);
    }
    else if(n<m){
        p=(m-n)*a;
        q=(n)*b;
        if(p>q){
            printf("%I64d",q);
        }
        else
            printf("%I64d",p);
    }

}

#include <stdio.h>
int main()
{
    long long int n,m,sum1,sum2;
    int a,b;
    scanf("%lld %lld %d %d",&n,&m,&a,&b);
    if(n%m==0)
        printf("0\n");
    else
    {
        int t;
        t=n%m;
        sum1=t*b;
        sum2=(m-t)*a;
        if(sum1>=sum2)
            printf("%lld\n",sum2);
        else
            printf("%lld\n",sum1);
    }
    return 0;
}

#include<stdio.h>
#define ll long long int
int main()

{
    ll n,m,a,b;
    scanf("%lld %lld %lld %lld",&n,&m,&a,&b);
    if(n%m==0)
        printf("0\n");
    else
    {
        if(n>m)
        {
            if((m-(n%m))*a<(m-(n%m))*b)
                printf("%lld\n",(m-(n%m))*a);
            else
                printf("%lld\n",(m-(n%m))*b);
        }
        else
        {
            if((m-n)*a>n*b)
                printf("%lld\n",n*b);
            else
                printf("%lld\n",(m-n)*b);
        }
    }
    return 0;
}

#include <stdio.h>

int main()
{
	int n,m,a,b,r,s;

	scanf("%d%d%d%d",&n,&m,&a,&b);

	if(n%m == 0)
	{
		printf("0\n");
	}
	else
	{
		s = n % m * b > (m - n % m) * a ?(m - n % m) * a:n % m * b;

		s = s > n * b?n * b:s;

		printf("%d",s);
	}

	return 0;
}

#include <stdio.h>

int main(void) {
	long long int n,m,a,b,c,d,cc,dd;
	scanf("%lld%lld%lld%lld",&n,&m,&a,&b);
	if(n>m)
	{
		d=n%m;
		dd=d*b;
		c=(((n/m)+1)*m)-n;
		cc=c*a;
	}
	else if(m>n)
	{
		dd=n*b;
		cc=(m-n)*a;
	}
	if(dd>cc)printf("%lld\n",cc);
	else if(cc>dd)printf("%lld\n",dd);
	else if (n%m==0)printf("0\n");

	return 0;
}

#include <stdio.h>

int main()
{
    int a , b, n, m, c, x, y, z;
    scanf("%d %d %d %d", &n, &m, &a, &b);

    c = n / m;

    if(n%m == 0)
    {
        printf("0\n");
        return 0;
    }

    x = (((c + 1)*m )- n)*a ;
    y = (n-(c*m))*b;
    if(x < y)
        z = x;
    else
        z = y;
    printf("%d\n", z);

    return 0;

}

//
//
#include <stdio.h>

int main()
{
    long long int n,i,m,a,b;
    scanf("%I64d %I64d %I64d %I64d",&n,&m,&a,&b);
    i=n%m;
    if(i==0)
    printf("0\n");
    else if(a*i<=b*(m-i))
    printf("%I64d\n",a*i);
    else
    printf("%I64d\n",b*(m-i));
	return 0;
}

#include<stdio.h>
#include<math.h>

int main()
{

    long long int m,n,a,b,p,q;
    scanf("%I64d %I64d %I64d %I64d",&n,&m,&a,&b);
    if( n%m==0){
        printf("0");
    }
    else if(n>m && n%m!=0){
        p=(n-m)*b;
        q=(m*(ceil(n/(m*1.0)))-n)*a;
        if(p>q){
            printf("%I64d",q);
        }
        else
            printf("%I64d",p);
    }
    else if(n<m){
        p=(m-n)*a;
        q=(n)*b;
        if(p>q){
            printf("%I64d",q);
        }
        else
            printf("%I64d",p);
    }

}

#include<stdio.h>
int main()
{
    long long int i, j, a, b, n, m, sum = 0;
    scanf("%lld %lld %lld %lld", &n, &m, &a, &b);
    if(n % m != 0)
    {
        if(n < m)
            sum = n*b;
        else
            sum = a*(2*m - n);
    }
    else
        sum = 0;
    printf("%lld\n", sum);
}

#include<stdio.h>
int main(void)
{
	int a,b,m,n,sum1=0,sum2=0,sum=0;
	scanf("%d%d%d%d",&n,&m,&a,&b);
	if(n%m==0)
	{
		printf("0\n");
	}
	if(n%m!=0)
	{
		if(n>m)
		{
			sum1=(m-(n-m))*a;
			sum2=(n-m)*b;
			if(sum1<sum2)
			{
				printf("%d\n",sum1);
			}
			else
			{
				printf("%d\n",sum2);
			}
		}
		if(n<m)
		{
			sum1=(m-n)*a;
			sum2=n*b;
			if(sum1<sum2)
			{
				printf("%d\n",sum1);
			}
			else
			{
				printf("%d\n",sum2);
			}
		}
	}
}

#include<stdio.h>

int main()
{
	long long n,m;
	int a,b;
	long long anc,sum;
	int i;
	scanf("%I64d%I64d%d%d",&n,&m,&a,&b);

		anc=sum=0;
	      	if(n%m==0 )
		{
			printf("0\n");

		}
		else
		{
			if(n>m&& n%m!=0)
			{
			anc=b*(n-m);

			sum=(2*m-n)*a;
			}
			else
			if(n<m && n%m!=0)
			{
				anc=n*b;
				sum=(m-n)*a;
			}
				if(anc<sum)
				{
					printf("%I64d\n",anc);

				}
				else
				{
					printf("%ld\n",sum);

				}

		return 0;

	}
	return 0;
}

#include<stdio.h>
#include<stdlib.h>
#define min(x,y) x<y?x:y
int main()
{
    int m,n,a,b,ccost,dcost;
    scanf("%d%d%d%d",&n,&m,&a,&b);
    if(n==m) printf("0");
    else if(n>m)
    {
        int dr=n%m,cr=((n/m)+1)*m-n;
        printf("%d",min(cr*a,dr*b));
    }
    else
    {
       int diff=m-n;
       printf("%d",min(diff*a,n*b));
    }

    return 0;
}

#include <stdio.h>
int main()
{
	long long int a,b,c,d,e,f,g,h;
	scanf("%lld%lld%lld%lld",&a,&b,&c,&d);
	if(a%b==0)
	{
		printf("0\n");
		return 0;
	}
	else
	{
		if(a<b)
		{
			f=b-a;
		}
		else
		{
			e=a/b;
			e=e+1;
			f=(e*b)-a;
		}
			g=f*c;
			h=a*d;
			if(g<h)
			{
				printf("%lld\n",g);
				return 0;
			}
			else
		{
			printf("%lld\n",h);
			return 0;
		}
		}

}

#include<stdio.h>
int main()
{
    int n,m,a,b,c,d;
    scanf("%d%d%d%d",&n,&m,&a,&b);
    if(n>m)
    {
        if(n%m==0)
        printf("0");
        else
        {
        		c=a*(m-(n%m));
        		d=b*(n%m);
        		if(c<d)
        			printf("%d",c);
        		else
        			printf("%d",d);
        }
    }
    else
    {
        c=((m-n)*a);
        d=(n*b);
        if(c<d)
        printf("%d",c);
        else
        printf("%d",d);
    }
    return 0;
}

#include<stdio.h>
int main()
{
	long long n, m, a, b ;
	scanf("%I64d %I64d %I64d %I64d", &n, &m, &a, &b);
	if (n >= m) {
		long long x, y;
		x = n % m;
		y = n / m;
		if (b*x >= ((y + 1)* m-n)*a) printf("%I64d", ((y + 1)*m-n)*a);
		else printf("%I64d", b*x);
	}
	else if (n < m) {
		if (a*n >= (m - n)*b) printf("%I64d", a*n);
		else printf("%I64d", (m - n)*b);
	}
}

#include <stdio.h>

int main(void) {
    int n,m,a,b,x,a1,a2,ans;
    scanf("%d",&n,&m,&a,&b);
    if(n%m==0)
    {
        printf("0\n");
    }
    else
    {
        x=n%m;
        a1=(n-x)*b;
        a2=(n+m-x)*a;
        if(a1>a2)
        {
            printf("%d",a2);
        }
        else
        {
            printf("%d",a1);
        }
    }
	// your code goes here
	return 0;
}

#include<stdio.h>
int main()
{

	long long int m,n,a,b;
	while(scanf("%lld%lld%lld%lld",&m,&n,&a,&b)==4)
	{
		long long j,k;
		if(m%n==0)
		printf("0\n");
		else{
			j=((2*n)-m)*a;
			k=(m%n)*b;
			if(j>k)
			j=k;
			printf("%lld\n",j);
		}
	}
	return 0;
}

#include<stdio.h>

int main()
{
	long long int m,n,a,b;
	int l,d;

	while(scanf("%lld%lld%lld%lld",&m,&n,&a,&b)!=EOF)
	{
		if(m%n==0)
		{
			printf("0\n");
		}
		else
		{
			l=(n-(m%n))*a;
			d=(m%n)*b;

			if(l<d)
			{
				printf("%d\n",l);
			}
			else
			{
				printf("%d\n",d);
			}
		}

	}

	return 0;
}

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#include<time.h>

int main()
{
    long long int n,m,a,b,r,i,j;
    scanf("%I64 %I64 %I64 %I64",&n,&m,&a,&b);
    if(n%m==0)
        printf("0");
    else if(n%m!=0)
    {
     r=n%m;
      i=r*b;
      j=(m-r)*a ;
      if(i<j)
        printf("%I64",i);
      else printf("%I64",j);
    }
}

#include<stdio.h>
int main()
{
    int n,m,a,b,ans,c1,c2,yu;
    scanf("%d %d %d %d",&n,&m,&a,&b);
    yu=n%m;
    if(yu==0)
        printf("0\n");
    else
    {
        c1=yu*b;
        c2=(m-yu)*a;
        if(c1>c2)
            printf("%d\n",c2);
        else
            printf("%d\n",c1);
    }
    return 0;
}

#include<stdio.h>
#include<math.h>

int main()
{
    long long int n,m,a,b,result,temp;

    scanf("%lld%lld%lld%lld",&n,&m,&a,&b);
    temp=n;
    if(n%m==0)
        {
            printf("0\n");
            return 0;
        }
    else
    {
        if(n>m)
        {
            while(1)
            {
                if(n%m==0) break;

                n++;

            }
        result=(n-temp)*a;
        printf("%%I64d\n",result);
        return 0;
        }

        else if(n<m)
        {
            result=(n)*b;
            printf("%%I64d",result);
            return 0;
        }

    }

}

#include<stdio.h>

int main()
{
	int m,n,a,b,l,d;

	while(scanf("%d%d%d%d",&m,&n,&a,&b)!=EOF)
	{
		if(m%n==0)
		{
			printf("0");
		}
		else
		{
			l=(n-m%n)*a;
			d=(m%n)*b;

			if(l<d)
			{
				printf("%d",l);
			}
			else
			{
				printf("%d",d);
			}
		}

	}

	return 0;
}

#include<stdio.h>

int main()
{
    long long n,m,a,b,p,q,l;
    scanf("%lld%lld%lld%lld",&n,&m,&a,&b);
    if(m>n)
    {
    p=n*b;
    q=(m-n)*b;
    if(p>q)
    printf("%lld",q);
    else
    printf("%lld",p);
    //printf("%lld",n*b);
    }
    else if(n>m)
    {
    l=n/m;
    p=(n-(m*l))*b;
    q=(((l+1)*m)-n)*a;
    if(p>q)
    printf("%lld",q);
    else
    printf("%lld",p);
    }
    else
    printf("%d",0);
    return 0;
}

#include<stdio.h>
#define min(x,y) x<y?x:y
int main()
{
int n,m,a,b,x,y;
scanf("%d %d %d %d",&n,&m,&a,&b);
x=(m-(n%m))*a;
y=(n%m)*b;
printf("%d",min(x,y));
return 0;
}

#include<stdio.h>
int main()
{
	long long int n,m,a,b,c,d,f;

	while(scanf("%lld%lld%lld%lld",&n,&m,&a,&b)==4)
	if(n%m==0)
	{
		printf("0");
	}
	else if(n<m)
	{
		c=b*n;
		d=(m-n)*a;
		if(c>d)
		{
			printf("%d",d);
		}
		else
		{
			printf("%d",c);
		}
	}
	else if(n>m)
	{
		f=n%m;
		c=a*(m-f);//jianli
		d=b*f;
		if(c>d)
		{
			printf("%d",d);
		}
		else
		{
			printf("%d",c);
		}
	}
	return 0;
}

#include<stdio.h>
#include<math.h>
int main()
{
	long long n,m;
	int a,b;
	long long anc,sum;
	int i;
	while(scanf("%ld%ld%d%d",&n,&m,&a,&b)==4)
	{
		anc=sum=0;
	      	if(n%m==0)
		{
			printf("0\n");

		}
		else
		{
			if(n>m)
			{
			anc=b*(n-m);

			sum=(2*m-n)*a;
			}
			else
			if(n<m)
			{
				anc=n*b;
				sum=(m-n)*a;
			}
				if(anc<sum)
				{
					printf("%ld\n",anc);

				}
				else
				{
					printf("%ld\n",sum);

				}

		}

	}
	return 0;
}

#include<stdio.h>
#include<stdlib.h>
#define min(x,y) x<y?x:y
int main()
{
    int m,n,a,b,ccost,dcost;
    scanf("%d%d%d%d",&n,&m,&a,&b);
    if(n==m) printf("0");
    else if(n>m)
    {
        int dr=n%m,cr=((cr/m)+1)*m-n;
        printf("%d",min(cr*a,dr*b));
    }
    else
    {
       int diff=m-n;
       printf("%d",min(diff*a,n*b));
    }

    return 0;
}

#include<stdio.h>
#include<math.h>

int main()
{
	long int n,m;
	int a,b;

	scanf("%ld%ld%d%d",&n,&m,&a,&b);

	long int r1,r2,c1,c2;

	if(n%m==0)
	{
		printf("0\n");
	}

	else
	{
		r1=n%m;
		r2=m-r1;

		if(r1<r2)
		{
	        	c1=r1*b;
	        	c2=r2*a;

		       if(c1<c2)
		       printf("%ld\n",c1);

		       else
		       printf("%ld\n",c2);
	    }

	    else
	    {
	    	    c1=r2*a;
	        	c2=r1*b;

		       if(c1<c2)
		       printf("%ld\n",c1);

		       else
		       printf("%ld\n",c2);
		}
	}
	return 0;

}

#include<stdio.h>
int main()
{
    int n,m,a,b,c,d;
    scanf("%d%d%d%d",&n,&m,&a,&b);
    if(n>m)
    {
        if(n%m==0)
        printf("0");
        else
        {
        		c=a*(m-(n%m));
        		d=b*(n%m);
        		if(c<d)
        			printf("%d",c);
        		else
        			printf("%d",d);
        }
    }
    else
    {
        c=((m-n)*a);
        d=(n*b);
        if(c<d)
        printf("%d",c);
        else
        printf("%d",d);
    }
    return 0;
}

#include<stdio.h>
int main()
{
    int m,n,a,b,ccost,dcost;
    scanf("%d%d%d%d",&n,&m,&a,&b);
    if(n==m) printf("0");
    else if(n>m)
    {
        int cr=n%m;
        printf("%d",cr*a>cr*b?cr*b:cr*a);
    }
    else
    {
        int cr=m%n;
        printf("%d",cr*a>cr*b?cr*b:cr*a);
    }

    return 0;
}

#include<stdio.h>
main()
{
    long int n,m,a,b,p,q,cntd=0,cntb=0,i,j,k,d=0,b1=0,flag=0;
    scanf("%ld%ld%ld%ld",&n,&m,&a,&b);
    p=n;q=n;
    while(1)
    {
        if(q%m!=0)
        {
            q=q-1;cntd++;
        }
        else break;
    }d=cntd*b;
    while(1)
    {  if(b1>d){flag=1;break;}
        if(p%m!=0)
        {
            p=p+1;cntb++;
            b1=cntb*a;
        }
        else break;
    }

    if(flag==0)printf("%ld\n",b1);
    else printf("%ld\n",d);
    return 0;

}

#include<stdio.h>
int main()
{
    long long int a,b,c,n,m;
    scanf("%lld %lld %lld %lld",&n,&m,&a,&b);
    if(n%m==0){
        printf("0\n");
    }
    else{
        if(n<m){
            printf("%lld\n",n*b);
        }
        else if(n>m){
            c=(m*2)-n;
            printf("%lld\n",c*a);
        }
    }

}

#include<stdio.h>

int main()
{
  int n,m,a,b,acost,bcost,t;
  scanf("%d%d%d%d",&n,&m,&a,&b);
  if(n%m==0)
   return printf("0\n"),0;
  else
  {
      t=n%m;
      acost=(m-t)*a;
      bcost=t*b;

      if(bcost<acost)
        return printf("%d\n",bcost),0;
  }
    printf("%d\n",acost);
}

#include<stdio.h>

int main()

{
    int n,m,a,b,c,d;

    scanf("%d %d %d %d",&n,&m,&a,&b);

    d=(n%m)*b;

    c=(((ceil(1.0*n/m))*m)-n)*a;

    (c<d)?printf("%d",c):printf("%d",d);
}

#include <stdio.h>
int main()
{
    long long int n,m,sum1,sum2;
    int a,b;
    scanf("%lld %lld %d %d",&n,&m,&a,&b);
    if(n%m==0)
        printf("0\n");
    else
    {
        int t;
        t=n%m;
        sum1=t*b;
        sum2=(m-t)*a;
        if(sum1>=sum2)
            printf("%lld\n",sum2);
        else
            printf("%lld\n",sum1);
    }
    return 0;
}

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define ll long long
int main(void)
{
    ll n,m,a,b;
    ll c1,c2,t;
    scanf("%lld%lld%lld%lld",&n,&m,&a,&b);

    if(n%m==0) printf("0");
    else
    {
        if(n>m)
        {
            c1=(n-m)*b;
            c2=(ceil(n*1./m)*m-n)*a;
            t=(c1>=c2)?c2:c1;
            printf("%lld",t);
        }
        else
        {
            c1=(m-n)*a;
            c2=n*b;
            t=(c1>=c2)?c2:c1;
            printf("%lld",t);
        }
    }

    return 0;
}

#include <stdio.h>

int main(void) {
    int n,m,a,b,x,a1,a2;
    scanf("%d %d %d %d",&n,&m,&a,&b);
   // printf("%d %d %d %d",n,m,a,b);
    if(n%m==0)
    {
        printf("0\n");
    }
    else
    {
        x=n%m;
        a1=(x)*b;
        a2=(m-x)*a;
        if(a1>a2)
        {
            printf("%d",a2);
        }
        else
        {
            printf("%d",a1);
        }
    }
   // printf("%d",x);
    //printf("%d %d",a1,a2);
	// your code goes here
	return 0;
}

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int min(int a, int b){
    return a<b ? a : b;
}

int main(){

    int i=0;

    int n,m,a,b;
    scanf("%d %d %d %d",&n,&m,&a,&b);

    int p = n%m;
    int q = m-p;

    int ans = min(b*p,a*q);

    printf("%d\n",ans);

    return 0;

}

#include<stdio.h>

int main()
{
	long long n,m;
	int a,b;
	long long anc,sum;
	int i;
	scanf("%ld%ld%d%d",&n,&m,&a,&b);

		anc=sum=0;
	      	if(n%m==0 )
		{
			printf("0\n");

		}
		else
		{
			if(n>m&& n%m!=0)
			{
			anc=b*(n-m);

			sum=(2*m-n)*a;
			}
			else
			if(n<m && n%m!=0)
			{
				anc=n*b;
				sum=(m-n)*a;
			}
				if(anc<sum)
				{
					printf("%ld\n",anc);

				}
				else
				{
					printf("%ld\n",sum);

				}

		return 0;

	}
	return 0;
}

#include<stdio.h>
int main()
{
    long long int a,b,c,d,x,y,n,m;
    scanf("%lld %lld %lld %lld",&n,&m,&a,&b);
    if(n%m==0){
        printf("0\n");
    }
    else{
        if(n<m){
            x=n*b;
            y=(m-n)*a;
            if(x>y)
            printf("%lld\n",y);
            else
            printf("%lld\n",x);
        }
        else if(n>m){
            c=(m*2)-n;
            d=(n-m)*b;
            if(c>d)
            printf("%lld\n",d);
            else
            printf("%lld\n",c);
        }
    }

}

#include<stdio.h>
#define min(x,y) x<y?x:y
int main()
{
int n,m,a,b,x,y;
scanf("%d %d %d %d",&n,&m,&a,&b);
x=(m-(n%m))*a;
y=(n%m)*b;
printf("%d",min(x,y));
return 0;
}

#include <stdio.h>

int main(void) {
	long long n,m,a,b,c,d,cc,dd;
	scanf("%lld%lld%lld%lld",&n,&m,&a,&b);
	if(n>m)
	{
		d=n%m;
		dd=d*b;
		c=(((n/m)+1)*m)-n;
		cc=c*a;
	}
	else if(m>n)
	{
		dd=n*b;
		cc=(m-n)*a;
	}
	if(dd>cc)printf("%d\n",cc);
	else if(cc>dd)printf("%d\n",dd);
	else if (n%m==0)printf("0\n");

	return 0;
}

#include<stdio.h>
#include<math.h>
#define Min(a,b) a<b ? a : b
int main()
{
    int n, m, a, b, d, x, y;
    scanf("%d%d%d%d",&n,&m,&a,&b);
    if(n%m==0)
    {
        printf("0\n");
        return 0;
    }
    d = (m+n-1)/m;
    x = (d*m - n)*a;
    d = Min(n,abs(n-m));
    y = d*b;
    printf("%d\n",Min(x,y));
    return 0;
}

#include <stdio.h>

int main()
{
	int n,m,a,b,r,s;

	scanf("%d%d%d%d",&n,&m,&a,&b);

	if(n%m == 0)
	{
		printf("0\n");
	}
	else
	{
		s = n % m * b > (m - n % m) * a ?(m - n % m) * a:n % m * b;

		printf("%d\n",s);
	}

	return 0;
}

#include<stdio.h>
int main()
{
    long long n,m,cost1,cost2;
    int a,b;
    scanf("%I64d %I64d %d %d",&n,&m,&a,&b);
    if(n%m==0)
        printf("0");
    else
    {
        if(n>m){
        cost1=((n/m+1)*m-n)*a;
        cost2=(n-(n/m-1)*m)*b;
        }
        else
        {
            cost1=(m-n)*a;
            cost2=n*b;
        }
    if(cost1<=cost2)
        printf("%I64d",cost1);
    else
        printf("%I64d",cost2);
    }
    return 0;
}

#include<stdio.h>
int main()
{
    int m,n,a,b,ccost,dcost;
    scanf("%d%d%d%d",&n,&m,&a,&b);
    if(n==m) printf("0");
    else if(n>m)
    {
        int cr=n%m;
        printf("%d",cr*a>cr*b?cr*b:cr*a);
    }
    else
    {
        int cr=m%n;
        printf("%d",cr*a>cr*b?cr*b:cr*a);
    }

    return 0;
}

#include<stdio.h>
#include<math.h>

int main()
{
    long long int n,m,a,b,result,temp,tenp2;

    scanf("%lld%lld%lld%lld",&n,&m,&a,&b);
    temp=n;tenp2=m;
    if(n%m==0)
        {
            printf("0\n");
            return 0;
        }
    else
    {
        if(n>m)
        {
            while(1)
            {
                if(n%m==0) break;

                n++;

            }
            printf("%d\n",n/m);
        result=((n-temp)*a);
        printf("%lld\n",result);
        return 0;
        }

        else if(n<m)
        {
            while(1)
            {
                if(n%m==0) break;

                n--;

            }
            result=(temp-n)*b;
            printf("%lld",result);
            return 0;
        }

    }

}


Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.

The first line contains two integers n and k (1 ≤ n, k ≤ 109) — the number of elements in the permutation and the lexicographical number of the permutation.

If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.

NoteA permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 ≤ i ≤ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 ≤ i ≤ n), that ai < bi, and for any j (1 ≤ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.In the first sample the permutation looks like that:1 2 3 4 6 7 5The only suitable position is 4.In the second sample the permutation looks like that:2 1 3 4The only suitable position is 4.

/* practice with Dukkha */
#include <stdio.h>
#include <string.h>

#define N	13

long long lucky(int b) {
	return b == 1 ? 0 : lucky(b / 2) * 10 + (b % 2 == 0 ? 4 : 7);
}

int is_lucky(int y) {
	return y == 0 ? 1 : is_lucky(y / 10) && (y % 10 == 4 || y % 10 == 7);
}

int ff[N];

void init() {
	int i, f;

	f = 1;
	for (i = 0; i < N; i++) {
		ff[i] = f;
		f *= i + 1;
	}
}

int get(int n, int k, int i) {
	static char used[N];
	int h, j;

	memset(used, 0, n * sizeof *used);
	for (h = 0; h < n; h++) {
		int f = ff[n - 1 - h], s = k / f;

		for (j = 0; j < n; j++)
			if (!used[j] && s-- == 0) {
				if (h == i)
					return j;
				used[j] = 1;
				break;
			}
		k %= f;
	}
	return -1;
}

int main() {
	int n, n_, k, b, ans;
	long long f;

	init();
	scanf("%d%d", &n, &k);
	f = 1;
	n_ = 1;
	while (f < k)
		f *= ++n_;
	if (n_ > n) {
		printf("-1\n");
		return 0;
	}
	ans = 0;
	for (b = 2; ; b++) {
		long long x = lucky(b);
		int y;

		if (x > n)
			break;
		if (x <= n - n_) {
			ans++;
			continue;
		}
		y = n - n_ + get(n_, k - 1, x - (n - n_) - 1) + 1;
		if (is_lucky(y))
			ans++;
	}
	printf("%d\n", ans);
	return 0;
}

#include <stdio.h>
#include <stdlib.h>
#define min(a, b) ((a < b) ? (a) : (b))
#define max(a, b) ((a > b) ? (a) : (b))

int l = 1, r, answ, t = 1;
long long a[11];
long long ans[2048];
int b[13], use[13];
int n, m, k;

int
analyze(void)
{
    int i, nonzero = 0;
    long long tmp = 0;
    for(i = 10; i >= 0; --i){
        if(a[i] == 0 && nonzero){
            return 1;
        }
        tmp = tmp * 10 + a[i];
        if (a[i] != 0){
            nonzero = 1;
        }
    }
    if(tmp >= r){
        ans[n++] = tmp;
        return 0;
    }
    if(tmp >= l){
        ans[n++] = tmp;
    }
    return 1;
}

void
go(int i)
{
    if(!t){
        return;
    }
    if (i == -1){
        t = analyze();
        return;
    }
    a[i] = 0;
    go(i - 1);
    a[i] = 4;
    go(i - 1);
    a[i] = 7;
    go(i - 1);
}

void
gen(int n, int p, int k){
if(p > 0){
    int i, ans = 1;
    for(i = 1; i <= p - 1; ++i){
        ans *= i;
    }
    int tmp = k / ans;
    if(k % ans == 0){
        --tmp;
    }
    if (tmp >= n){
        printf("-1");
        m = -1;
        return;
    }
    for(i = 0; ; ++i){
        if(use[i] == 0){
            if(tmp > 0){
                --tmp;
            } else {
                use[i] = 1;
                b[n - p] = i;
                break;
            }
        }
    }
    gen(n, p - 1, k % ans ? k % ans : ans);
}
}

int
main(void)
{
    scanf("%d%d", &m, &k);
    r = m;
    go(10);
    answ = 0;
    int i, j, q;
    for(i = 0; ans[i] <= m - 13; ++i){
        ++answ;
    }
    gen(min(13, m), min(13, m), k);
    if(m == -1){
        return 0;
    }
    for(i = 0; i < min(13, m); ++i){
        b[i] += max(1, m - 12);
    }
    for(i = max(1, m - 12); i <= m; ++i){
        for(j = 0; j < n; ++j){
            if(ans[j] == i){
                for(q = 0; q < n; ++q){
                    if(b[i - max(1, m - 12)] == ans[q]){
                        ++ answ;
                    }
                }
            }
        }
    }
    printf("%d", answ);
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#define min(a, b) ((a < b) ? (a) : (b))
#define max(a, b) ((a > b) ? (a) : (b))

int l = 1, r, answ, t = 1;
long long a[11];
long long ans[2048];
int b[13], use[13];
int n, m, k;

int
analyze(void)
{
    int i, nonzero = 0;
    long long tmp = 0;
    for(i = 10; i >= 0; --i){
        if(a[i] == 0 && nonzero){
            return 1;
        }
        tmp = tmp * 10 + a[i];
        if (a[i] != 0){
            nonzero = 1;
        }
    }
    if(tmp >= r){
        ans[n++] = tmp;
        return 0;
    }
    if(tmp >= l){
        ans[n++] = tmp;
    }
    return 1;
}

void
go(int i)
{
    if(!t){
        return;
    }
    if (i == -1){
        t = analyze();
        return;
    }
    a[i] = 0;
    go(i - 1);
    a[i] = 4;
    go(i - 1);
    a[i] = 7;
    go(i - 1);
}

void
gen(int n, int p, int k){
if(p > 0){
    int i, ans = 1;
    for(i = 1; i <= p - 1; ++i){
        ans *= i;
    }
    int tmp = k / ans;
    if(k % ans == 0){
        --tmp;
    }
    for(i = 0; ; ++i){
        if(use[i] == 0){
            if(tmp > 0){
                --tmp;
            } else {
                use[i] = 1;
                b[n - p] = i;
                break;
            }
        }
    }
    gen(n, p - 1, k % ans ? k % ans : ans);
}
}

int
main(void)
{
    scanf("%d%d", &m, &k);
    r = m;
    go(10);
    answ = 0;
    int i, j, q;
    for(i = 0; ans[i] <= m - 13; ++i){
        ++answ;
    }
    gen(min(13, m), min(13, m), k);
    for(i = 0; i < min(13, m); ++i){
        b[i] += max(1, m - 12);
    }
    for(i = max(1, m - 12); i <= m; ++i){
        for(j = 0; j < n; ++j){
            if(ans[j] == i){
                for(q = 0; q < n; ++q){
                    if(b[i - max(1, m - 12)] == ans[q]){
                        ++ answ;
                    }
                }
            }
        }
    }
    printf("%d", answ);
    return 0;
}