#include <stdio.h>

int CalculaMax (int tipo, int tot[][4], int massa);

int main (){
	int tot[11][4], i, max;

	scanf ("%d %d %d %d", &tot[0][0], &tot[0][1], &tot[0][2], &tot[0][3]);
	for (i = 1; i <= tot[0][1]; i++)
		scanf ("%d %d %d %d", &tot[i][0], &tot[i][1], &tot[i][2], &tot[i][3]);

	max = CalculaMax (0, tot, tot[0][0]);

	printf ("%d\n", max);
	return 0;

}

int CalculaMax (int tipo, int tot[][4], int massa){
	int maxpaes, i, totalparcial, total = 0;

	if (tipo > tot[0][1])
		return 0;

	maxpaes = massa/tot[tipo][2];
	if (tipo != 0)
		if (tot[tipo][0]/tot[tipo][1] < maxpaes)
			maxpaes = tot[tipo][0]/tot[tipo][1];

	for (i = maxpaes; i >= 0; i--){
		totalparcial = i*tot[tipo][3] + CalculaMax (tipo+1, tot, massa-(i*tot[tipo][2]));
		if (totalparcial > total)
			total = totalparcial;
	}

	return total;

}


#include <stdio.h>

#define N 101

int coords[N] = {0};
int help[N] = {0};
int help2[N] = {0};
int help3[N] = {0};

int Finder(int n)
{
  int i, j, k, p;
  int maxCounter = 0;
  int counter = 0;
  int isStarted = 0;
  int currentColor = -1;
  int maxIndex = -1;

  for (i = 0; i < n + 1; ++i)
    help3[i] = 0;

  for (i = 0; i < n + 1; ++i)
  {
    if (help2[i] == 0)
      break;

    if (isStarted == 0)
      currentColor = help2[i];

    if (help2[i] == currentColor)
    {
      ++counter;
      isStarted = 1;
    } else
    {
      if (counter > maxCounter)
      {
        maxIndex = i - maxCounter;
        maxCounter = counter;
      }

      counter = 0;
      isStarted = 0;
    }
  }

  if (counter > maxCounter)
  {
    maxIndex = i - maxCounter + 1;
    maxCounter = counter;
  }

  if (counter >= 3)
  {
    for (k = maxIndex; k < maxIndex + maxCounter; ++k)
    {
      help2[i] = -1;
    }

    for (i = 0, k = 0; i < n + 1; ++i)
    {
      if (help2[i] == -1)
        continue;

      help3[k++] = help2[i];
    }

    for (i = 0; i < n + 1; ++i)
      help2[i] = help3[i];

    return 1;
  }

  return 0;
}

int main(void)
{
  int p;
  int i, j, k;
  int n, x;
  int tmp;
  int l, r;
  int maxPoint = -1000, minPoint = 1000;
  int maxLen = 0;
  int result = 0;
  int maxresult = 0;
  int maxCounter = 0;
  int counter = 0;

  scanf("%i %i %i", &n, &k, &x);

  for (i = 0; i < n; ++i)
  {
    scanf("%i", &tmp);
    coords[i] = tmp;
    help[i] = tmp;
  }

  for (i = 0; i < n; ++i)
  {
    if (coords[i] == x)
    {
      for (k = 0, p = 0; k < n + 1; ++k)
      {
        if (i == k)
        {
          help2[k] = x;
          continue;
        }

        help2[k] = coords[p++];
      }
    }

    while (1 == Finder(n))
      ;

    for (k = 0; k < n + 1; ++k)
    {
      if (help2[k] == 0)
        break;
    }

    counter = n - k;

    if (counter > maxCounter)
      maxCounter = counter;
  }

  printf("%i\n", maxCounter);

  return 0;
}


#include <stdio.h>

#define GREEN 0
#define RED 1

int main()
{
  double l, d, v, g, r;
  double time;
  double sv_time;
  int sv_flag;

  scanf("%lf %lf %lf %lf %lf", &l, &d, &v, &g, &r);

  time = d/v;

  sv_time = 0;
  sv_flag = RED;
  while (sv_time < time)
  {
    sv_flag = sv_flag == GREEN ? RED : GREEN;
    sv_time += sv_flag == GREEN ? g : r;
  }

  if (sv_flag == RED)
    time = sv_time;

  time += (l - d) / v;

  printf("%.8lf\n", time);

  return 0;
}


#include<stdio.h>
double tra();
double l,d,v,g,r,t;
void main()
{
	scanf("%lf%lf%lf%lf%lf",&l,&d,&v,&g,&r);
	t=l/v+tra(d/v,g,r);
	printf("%.8lf",t);
}
double tra()
{
	double sum=0;
	for (int i=0;;i++)
	{
			if (i%2==0){sum+=g;}
	        else { sum+=r;}
	    		if (sum>=d/v)
	    		{
			if (i%2==0){return 0;}
			else {return sum-d/v;}
		      }
	 }
}


#include<stdio.h>
double tra();
double l,d,v,g,r,t;
int main()
{
	scanf("%lf%lf%lf%lf%lf",&l,&d,&v,&g,&r);
	t=l/v+tra(d/v,g,r);
	printf("%.8lf",t);
}
double tra()
{
	double sum=0;
	for (int i=0;;i++)
	{
			if (i%2==0){sum+=g;}
	        else { sum+=r;}
	    		if (sum>=d/v)
	    		{
			if (i%2==0){return 0;}
			else {return sum-d/v;}
		      }
	 }
}


#include <stdio.h>

int main()
  {
  	float ans;
  	int l, d, v, g, r, i;
  	scanf("%d %d %d %d %d", &l, &d, &v, &g, &r);
  	i = d/v;
  	if(i*v != d)
  	  i++;
  	if(i % (g+r) < g && i % (g+r) != 0)
  	  {
  	  	ans = (float) l/v;
	  }
	else
	  {
	  	ans = ( (float) (l-d)/v) + (d/v/(g+r)+1) * (g+r);
	  }
	printf("%f", ans);
	getchar();
	getchar();
	return 0;
  }


#include<stdio.h>
#include<math.h>
int main()
{
    int n,m,i,j,temp;
    scanf("%d %d",&m,&n);
    int arrM[m],arrN[n];

    for(i=0;i<m;i++)
    {
        scanf("%d",&arrM[i]);
    }
    for(i=0;i<n;i++)
    {
        scanf("%d",&arrN[i]);
    }
     for(i=0; i<m; i++)
    {
        for(j=i+1; j<m; j++)
        {
            if(arrM[i] > arrM[j])
            {
                temp     = arrM[i];
                arrM[i] = arrM[j];
                arrM[j] = temp;
            }
        }
    }
     for(i=0; i<n; i++)
    {
        for(j=i+1; j<n; j++)
        {
            if(arrN[i] > arrN[j])
            {
                temp     = arrN[i];
                arrN[i] = arrN[j];
                arrN[j] = temp;
            }
        }
    }
    if(arrM[0]==arrN[0])
        printf("%d ",arrM[0]);
    else
    if(arrM[0]>arrN[0])
    printf("%d ",(arrN[0]*10)+arrM[0]);

    else
            printf("%d ",(arrM[0]*10)+arrN[0]);
    return main();
}


#include <stdio.h>
int main()
{
   int n, m,no=1;
   scanf("%d %d", &n,&m);
   int a[n];
   int b[m];
   int i,nn=9,mm=9;
   for(i=0;i<n;i++)
   {
       scanf("%d", &a[i]);
       if(a[i]<nn)
       nn=a[i];
   }
   for(i=0;i<m;i++)
   {
       scanf("%d", &b[i]);
       if(b[i]<mm)
       mm=b[i];
   }
   for(i=0;i<n;i++)
   for(j=0;j<m;j++)
   if(a[i]==b[j])
   {
       printf("%d", a[i]);
       no=0;
       break;
   }
   if(no==1)
   {
   if(nn==mm)
   printf("%d", nn);
   else if(nn>mm)
   printf("%d%d", mm, nn);
   else
   printf("%d%d", nn, mm);
   }
    return 0;
}


#include<stdio.h>
int main()
{
 int a,b,f=0,d=0,c;
 scanf("%d %d",&a,&b);
 while(a!=0 || b!=0)
 {
    a=a-1;
    b=b-1;
    f=f+1;
 }
 if (a<b)
    c=b;
 else c=a;
 while(c>1)
 {
    c=c-2;
    d=d+1;
 }
 printf("%d %d",f,d);

}


#include <stdio.h>

int main()
{
	int num;
	scanf("%d", &num);

	if ( (num & 1) == 0 && num > 2)
	{
		printf("YES");
		return 1;
	}

	printf("NO");

	return 1;
}
/* 1523935235242 */


#include <stdio.h>
int main ()
{
    int w;
    scanf("%d", w);
    if (w % 2 == 0)
    {
        printf("YES\n");
    } else {
           printf("NO\n");
           }

    return 0;
}
/* 1491872661792 */


#include "stdio.h"
#include "stdlib.h"
#include "string.h"

int main(int argc, char const *argv[]) {
  unsigned long long int p,d,i,x,continuer = 1,y,a=0,b=1 ;
  char str[100000] ;
  scanf("%llu%llu",&p,&d );
  x = p-d ;
  /*sprintf(str, "%llu", p);
printf("%s\n",str );
y = atoll(str);
printf("%llu\n",y );*/

 while(continuer)
  {
    sprintf(str, "%llu", p);
    str[a]-- ;
    for(i=b;i<strlen(str);i++)
      str[i] = '9' ;
    y = atoll(str);
    if(y>=x)
      continuer = 0 ;
    else
    {

      a++;
      b++;
    }
    if(b==strlen(str) && str[b-1]-1 != 9)
      {printf("%llu\n",p );
      return 0 ;}
  }
  printf("%llu\n",y );
  return 0;
}


#include "stdio.h"
#include "stdlib.h"
#include "string.h"

int main(int argc, char const *argv[]) {
  unsigned long long int p,d,i,x,continuer = 1,y,a=0,b=1 ;
  char str[100000] ;
  scanf("%llu%llu",&p,&d );
  x = p-d ;
  /*sprintf(str, "%llu", p);
printf("%s\n",str );
y = atoll(str);
printf("%llu\n",y );*/

 while(continuer)
  {
    sprintf(str, "%llu", p);
    str[a]-- ;
    for(i=b;i<strlen(str);i++)
      str[i] = '9' ;
    y = atoll(str);
    if(y>x)
      continuer = 0 ;
    else
    {

      a++;
      b++;
    }
    if(b==strlen(str) && str[b-1]-1 != 9)
      {printf("%llu\n",p );
      return 0 ;}
  }
  printf("%llu\n",y );
  return 0;
}


#include <stdio.h>

int main() {
	long long p, d, x;
	int k;

	scanf("%lld%lld", &p, &d);
	x = 1;
	k = 0;
	while (p >= 10) {
		int t = p % 10;

		if (t != 9) {
			int a = t + 1;

			if (d >= a * x) {
				d -= a * x;
				k++;
				p = p / 10 - 1;
				x *= 10;
			} else
				break;
		} else {
			k++;
			p /= 10;
			x *= 10;
		}
	}
	printf("%lld", p);
	while (k--)
		printf("9");
	printf("\n");
	return 0;
}


#include <stdio.h>
int yh,ya,yd;
int mh,ma,md;
int h,a,d;

int lTime(int h, int a, int d) {
    int i=-1;
    if(d>=a) return 999999999;
    while(h>0){
        h=h-(a-d);
        i++;
    }
    return i;
}
int main() {
    int i,j,k,cost,min=999999999;
    scanf("%d %d %d",&yh,&ya,&yd);
    scanf("%d %d %d",&mh,&ma,&md);
    scanf("%d %d %d",&h,&a,&d);
    for(i=0;i<100;i++){
        for(j=0;j<100;j++) {
            for(k=0;k<100;k++) {
                if(lTime(yh+i,ma,yd+k)>lTime(mh,ya+j,md)) {
                    cost=i*h+j*a+k*d;
                    if(cost<min) {
                        min=cost;
//                      printf("%d %d %d %d\n",i,j,k,cost);
                    }
                }
            }
        }
    }
    printf("%d\n",min);
    return 0;
}


#include <stdio.h>

int lTime(int h, int a, int d) {
    int i=-1;
    if(d>=a) return 999999999;
    while(h>0){
        h=h-(a-d);
        i++;
    }
    return i;
}
int main() {
    int yh,ya,yd;
    int mh,ma,md;
    int h,a,d;
    int lh,la,ld;
    int i,j,k,cost,min=999999999,yaa=0;
    scanf("%d %d %d",&yh,&ya,&yd);
    scanf("%d %d %d",&mh,&ma,&md);
    scanf("%d %d %d",&h,&a,&d);

    lh=(lh>10)?8000/h:800;
    la=(la>10)?8000/a:800;
    ld=(ld>10)?8000/d:800;
    if(ya<=md) yaa=md-ya+1;
//  printf("%d %d %d\n",lh,la,ld);
    for(i=0;i<lh;i++){
        for(j=yaa;j<la;j++) {
            for(k=0;k<ld;k++) {
                if(lTime(yh+i,ma,yd+k)>lTime(mh,ya+j,md)) {
                    cost=i*h+j*a+k*d;
                    if(cost<min) {
                        min=cost;
//                      printf("%d %d %d %d\n",i,j,k,cost);
                    }
                }
            }
        }
    }
    printf("%d\n",min);
    return 0;
}


#include<stdio.h>
long long int lcm1(int,int);
int main()
{
    int x,y;
    long long int a,b,lcm,n,j;
    scanf("%d %d %I64d %I64d",&x,&y,&a,&b);
    lcm=(x*y)/lcm1(x,y);
    for(j=a;j<=b;j++)
    {
        if(j%x==0&&j%y==0)
            break;
    }
    n=(b-j)/lcm;
    if(j!=b+1)
    n++;
    printf("%I64d\n",n);
    return(0);
}
long long int lcm1(int x,int y)
{
    if(y==0)
        return((long long int)x);
    lcm1(y,x%y);
}


#include<stdio.h>
long int gcd(long int,long int);
long int sub(long int , long int );
int main()
{
	long int x,y,a,b,count=0,temp,i;

	scanf("%lu %lu %lu %lu",&x,&y,&a,&b);
	temp = x*y;

	temp = temp/gcd(x,y);

	for(i=a;i<=b;i++)
	{
		if(i%temp==0)
		{
			count++;

		}
	}
	printf("%lu\n",count);

}
long int gcd (long int a,long int b)
{
	long int i;
	if(b==0)
		return a;
	else{
		i=gcd(b,sub(a,b));
		return i;
	}

}
long int sub(long int a, long int b)
{
	if(a>=b)
		return(a-b);
	else
		return(b-a);

}


#include<stdio.h>
long int gcd(long int,long int);
long int sub(long int , long int );
int main()
{
	long int x,y,a,b,count=0,temp,i;

	scanf("%lu %lu %lu %lu",&x,&y,&a,&b);
	temp = x*y;

	temp = temp/gcd(x,y);

	for(i=a;i<=b;i++)
	{
		if(i%temp==0)
		{
			count++;

		}
	}
	printf("%lu\n",count);

}
long int gcd (long int a,long int b)
{
	long int i;
	if(b<=0)
		return a;
	else{
		i=gcd(b,sub(a,b));
		return i;
	}

}
long int sub(long int a, long int b)
{
	if(a>=b)
		return(a-b);
	else
		return(b-a);

}


#include<stdio.h>
int main()
{
	int n,i,div,mod;
	long long int p=2,sum=-2;
	scanf("%d",&n);
	for(i=1;i<=n;i++)
		p=p*2;
	sum+=p;
	div=sum%1000000000;
	mod=sum/1000000000;
	if(mod>0)
		printf("%d",mod);
	printf("%09d\n",div);
	return 0;
}


#include<stdio.h>

typedef unsigned long long int lli;

void msort(lli a[], int s, int e)
{
    if(e-s!=0)
    {
        msort(a, s, (s+e)/2);
        msort(a, (s+e)/2 + 1, e);
    }
    else
        return;
    lli b[e-s+1];
    int  i=s, j=(s+e)/2 + 1, n;
    for(n=0; i<=(s+e)/2 && j<=e;n++)
    {
        if(a[i]<=a[j])
            b[n]=a[i++];
        else
            b[n]=a[j++];
    }
    if(i<=(s+e)/2)
        for(;i<=(s+e)/2;i++, n++)
            b[n] = a[i];
    else
        for(;j<=e;j++,n++)
            b[n]=a[j];
    for(i=0;i<n;i++, s++)
        a[s] = b[i];
}

int checklog10(lli a, lli b)
{
    lli i=0,j,k;
    while(a!=0)
    {
        a/=10;
        i++;
    }
    while(b!=0)
    {
        b/=10;
        i++;
    }
    if(i>18)
        return 1;
    return 0;
}

int main()
{
    lli i,j,ele;
    lli x,y,l,r;
    lli a[100000], b[100000];
    scanf("%llu %llu %llu %llu", &x, &y, &l, &r);
    if(y<x)
    {
        y = x+y;
        x = y - x;
        y = y - x;
    }

    int ct = 0, ind=0;
    i=1;
    while(i<r)
    {
        a[ind++] = i+1;
        if(checklog10(i, x)==1)
            break;
        i*=x;
    }
/*
    for(i=0;i<ind;i++)
        printf("%llu ", a[i]);
    putchar('\n');
  */
    int eind;
    eind = ind-1;

    j=1;

    while(j<r)
    {
        if(checklog10(j,y)==1)
            break;
        j*=y;
        ct=0;
        ele=0;
        while(ele<=r && ct<=eind)
        {
            ele = j+a[ct++]-1;

            if(ele>r)
            {
                break;
            }
            a[ind++] = ele;

    //        printf("%llu ", ele);

        }
  //      putchar('\n');
    }

    ct=0;
    for(i=0;i<ind;i++)
        if(a[i]>=l && a[i]<=r)
            b[ct++] = a[i];

    b[ct++] = l-1;
    b[ct++] = r+1;

    msort(b, 0, ct-1);
/*
    for(i=0;i<ct;i++)
      printf("%llu ", b[i]);
   putchar('\n');
*/
    lli  mxdif = 0;
    for(i=0;i<ct-1;i++)
    {
        if(b[i+1]-b[i]>mxdif)
        {
            mxdif = b[i+1]-b[i];
     //       printf("%lld %lld %lld\n",b[i], b[i+1], i);
        }
    }
    printf("%llu\n", mxdif-1);

    return 0;
}


#include<stdio.h>

int main()
{
    int pos,arr[]={0,1,2},temp,i;
    long long int n;
    scanf("%lld %d",&n,&pos);
    for(i=1;i<=n;i++)
    {
        if(i%2==0)
        {
            temp=arr[1];
            arr[1]=arr[2];
            arr[2]=temp;
        }
        else{
            temp=arr[0];
            arr[0]=arr[1];
            arr[1]=temp;
        }
    }
    printf("%d",arr[pos]);
}


#include <stdio.h>

#define NUM_SHELLS 3
#define LEFT 0
#define MIDDLE 1
#define RIGHT 2

int main(void) {
    int n, x;
    scanf("%d %d", &n, &x);

    int shells[NUM_SHELLS];
    for (int i = 0; i < NUM_SHELLS; i++) {
        if (i == x) {
            shells[i] = 1;
        } else {
            shells[i] = 0;
        }
    }

    while (n != 0) {
        if (n % 2 == 0) {
            int temp = shells[RIGHT];
            shells[RIGHT] = shells[MIDDLE];
            shells[MIDDLE] = temp;
        } else {
            int temp = shells[LEFT];
            shells[LEFT] = shells[MIDDLE];
            shells[MIDDLE] = temp;
        }
        n--;
    }

    for (int i = 0; i < NUM_SHELLS; i++) {
        if (shells[i] == 1) {
            printf("%d", i);
        }
    }

    return 0;
}


#include <stdio.h>
int main()
{
    long long n;
    int x,p;
    scanf("%I64d %d",n,x);

    if(n%2==0){
        if(n%3==0) p=x;
        else if(n%3==2){
            if(x==2) p=0;
            else p=x+1;
        }
        else{
            if(x==0) p=2;
            else if(x==1) p=0;
            else p=1;
        }
    }
    else{
        if(n%3==0){
            if(x==0) p=2;
            else if(x==2) p=0;
            else p=1;
        }
        else if(n%3==2){
            if(x==0) p==0;
            else if(x==1) p==2;
            else p==0;
        }
    }
    printf("%d",p);
    return 0;
}


#include<stdio.h>

void swap(int *a, int *b)
{
    int c = *a;
    *a = *b;
    *b = c;
}

int main()
{
    int n, x;
    scanf("%d%d", &n, &x);

    int arr[3];
    for(int i=0; i<3; i++)
    {
        arr[i] = 0;
    }
    arr[x] = 1;
    while(n>0)
    {
        if(n%2==0)
        {
            swap(&arr[1], &arr[2]);
        }
        else
            swap(&arr[0], &arr[1]);
        n = n-1;
    }

    for(int i=0; i<3; i++)
    {
        if(arr[i]==1)
            printf("%d", i);
    }

    return 0;

}


#include<stdio.h>
int main()
{
    long long int n;
    scanf("I64d",&n);
    printf(n+1);
    return 0;
}


#include <stdio.h>
int main(){
	long long n,k,i;
	int ans=1;
	scanf("%d %d",&n,&k);
	long long lb=-2;
	long long ub=k+2;
	if(n==1){
		puts("0");
		return 0;
	}
	while(ub-lb>1){
		long long mid=(lb+ub)/2;
		if((k+mid)*(k-mid+1)/2>n+(k-mid+1)){
			lb=mid;
		}else{
			ub=mid;
		}
	}
	for(i=ub+5;i>=lb-5;i--){
		if(i>=2 && i<=k){
			long long aw=k-i+1;
			long long sum=(k+i)*(k-i+1)/2;
			sum-=(aw-1);
			if(sum>=n){
				printf("%lld\n",k-i+1);
				return 0;
			}
		}
	}
	puts("-1");
}


#include <stdio.h>

typedef unsigned long long int LL;
LL highval(LL k, int digits) {
    LL sum = 0;
    for (int i = 0; i < digits; i++) {
        sum += k-i;
    }
    return sum-digits+1;
}
//LL lowval(LL k, int digits) {
LL lowval(int digits) {
    LL sum = 0;
    for (int i = 0; i < digits; i++) {
        sum += i+2;
    }
    return sum-digits+1;
}

LL cal(LL val, LL k) {
    int lo=0, hi=k; //val = [2..k]
    while (hi-lo > 1) {
    //printf("SHIT\n");
    //printf("%d %d\n", lo, hi);
        int mid = (hi+lo)/2;
        LL u = highval(k, mid);
        if (val > u)
            lo = mid;
        else
            hi = mid;
    }
    return (val >= lowval(hi) && val <= highval(k, hi)) ? hi : -1;
}

int main() {
    LL n, k;
    scanf("%llu %llu", &n, &k);
    printf("%llu\n", cal(n, k));
    return 0;
}


#include <stdio.h>

typedef long long LL;
LL highval(LL k, int digits) {
    LL sum = 0;
    int i;
    for (i = 0; i < digits; i++) {
        sum += k-i;
    }
    return sum-digits+1;
}

//LL lowval(LL k, int digits) {
LL lowval(int digits) {
    LL sum = 0;
    int i;
    for (i = 0; i < digits; i++) {
        sum += i+2;
    }
    return sum-digits+1;
}

LL cal(LL val, LL k) {
    int lo=0, hi=k; //val = [2..k]
    while (hi-lo > 1) {
    //printf("SHIT\n");
    //printf("%d %d\n", lo, hi);
        int mid = (hi+lo)/2;
        LL u = highval(k, mid);
        if (val > u)
            lo = mid;
        else
            hi = mid;
    }
    return (val >= lowval(hi) && val <= highval(k, hi)) ? hi : -1;
}

int main() {
    LL n, k;
    scanf("%lld %lld", &n, &k);
    printf("%lld\n", cal(n, k));
    return 0;
}


#include <stdio.h>

typedef long long LL;
LL highval(LL k, int digits) {
    /*
    LL sum = 0;
    int i;
    for (i = 0; i < digits; i++) {
        sum += k-i;
    }
    */
    LL sum = (k*(k+1) - (k-digits)*(k-digits+1))/2;
    return sum-digits+1;
}

//LL lowval(LL k, int digits) {
LL lowval(int digits) {
    /*
    LL sum = 0;
    int i;
    for (i = 0; i < digits; i++) {
        sum += i+2;
    }
    */
    LL sum = (digits+1)*(digits+2)/2 - 1;
    return sum-digits+1;
}

LL cal(LL val, LL k) {
    int lo=0, hi=k; //val = [2..k]
    while (hi-lo > 1) {
    //printf("SHIT\n");
    //printf("%d %d\n", lo, hi);
        int mid = (hi+lo)/2;
        LL u = highval(k, mid);
        if (val > u)
            lo = mid;
        else
            hi = mid;
    }
    return (val >= lowval(hi) && val <= highval(k, hi)) ? hi : -1;
}

int main() {
    LL n, k;
    scanf("%lld %lld", &n, &k);
    printf("%lld\n", cal(n, k));
    return 0;
}


#include <stdio.h>

char sayi[50];

void BubbleSort(int array_size)
{
    int i, j, temp;
    for (i = 0; i < (array_size - 1); ++i)
    {
        for (j = 0; j < array_size - 1 - i; ++j )
        {
            if (sayi[j] < sayi[j+1])
            {
                temp = sayi[j+1];
                sayi[j+1] = sayi[j];
                sayi[j] = temp;
            }
        }
    }
    for(i=0;i<array_size;i++)
    {
        if(sayi[i] != '1')
                printf("%c",sayi[i]);
    }
}
int main()
{
    int n,i=0;
    scanf("%d",&n);
    scanf("%s",sayi);
    while(1)
    {
        if(i >= n)
            break;
        if(sayi[i] == '1' || sayi[i] == '2' || sayi[i] == '3' || sayi[i] == '5')
        {
            i++;
        }
        else if(sayi[i] == '4')
        {
            sayi[i] = '3';
            sayi[n] = '2';
            sayi[n+1] = '2';
            n+=2;
            i++;
        }
        else if(sayi[i] == '6')
        {
            sayi[i] = '5';
            sayi[n] = '3';
            n++;
            i++;
        }
    }
    BubbleSort(n);
    return 0;
}


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

struct node {
	int value;
	struct node* next;
};

struct node* head;

void addNode(int val) {
	struct node* newnode = (struct node*) malloc(sizeof(struct node));
	newnode->value = val;
	newnode->next = NULL;
	if (head == NULL) {
		head = newnode;
	}
	else {
		struct node* temp = head;
		struct node* tempp = head;
		while (temp->value > val && temp != NULL) {
			tempp = temp;
			temp = temp->next;
		}
		if (temp == head) {
			newnode->next = head;
			head = newnode;
		}
		else if (temp == NULL) {
			tempp->next = newnode;
		}
		else {
			newnode->next = temp;
			tempp->next = newnode;
		}
	}
}

int main() {
	int n;
	scanf("%d\n", &n);

	int x;
	char ch;
	while (n-- > 0) {
		scanf("%c", &ch);
		x = ch - '0';
		if (x != 0 && x != 1) {
			if (x == 4) {
				addNode(3);
				addNode(2);
				addNode(2);
			}
			else if (x == 6) {
				addNode(5);
				addNode(3);
			}
			else if (x == 8) {
				addNode(7);
				addNode(2);
				addNode(2);
				addNode(2);
			}
			else if (x == 9) {
				addNode(7);
				addNode(3);
				addNode(3);
				addNode(2);
			}
			else {
				addNode(x);
			}
		}
	}

	while (head != NULL) {
		printf("%d", head->value);
		head = head->next;
	}

	return 0;
}


#include<stdio.h>
#include<string.h>
int h[10000];
void ekle(int x)
{
	int i=1;
	while(h[i])
	{
		if(x<=h[i])
			i=i*2;
		else if(x>h[i])
			i=2*i+1;
	}
	h[i]=x;
}
void yazdir(int i)
{
	if(!h[i])
		return;
	yazdir(2*i+1);
	printf("%d",h[i]);
	yazdir(2*i);
}

int main()
{
	int n,i;
	char a;
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		scanf(" %c",&a);
		switch(a)
		{
			case '2': ekle(2);break;
			case '3': ekle(3);break;
			case '4': ekle(3); ekle(2); ekle(2); break;
			case '5': ekle(5);break;
			case '6': ekle(5); ekle(3); break;
			case '7': ekle(7);break;
			case '8': ekle(7); ekle(2); ekle(2); ekle(2); break;
			case '9': ekle(7); ekle(3); ekle(3); ekle(2); break;

		}
	}
	yazdir(1);
	return 0;
}


#include<stdio.h>
int x,y,z;
int main()
{
    scanf("%d %d %d",&x,&y,&z);
    if(x+y+z==12)
    printf(6);
    else printf("%d",(x+y+z)/3);
}


#include <stdio.h>

int main(void)
	{
		int x,y,z;
		scanf("%d%d%d",&x,&y,&z);
		if (x == 7) {
			printf("6");
			return 1;}
		if ((x-y)*(x-z)<0) printf("%d",x);
		if ((y-x)*(y-z)<0) printf("%d",y);
		if ((z-x)*(z-y)<0) printf("%d",z);
	}


void main(){
int x,y,a,b,i,count = 0;
scanf ("%d %d %d %d",&x,&y,&a,&b);
for (i=a;i<b+1;i++)
{
    if (i%x == 0 && i%y == 0)
        count++;
}

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

}


#include<stdio.h>
int main(){
int x,y,a,b,i,count = 0;
scanf ("%d %d %d %d",&x,&y,&a,&b);
for (i=a;i<b+1;i++)
{
    if (i%x == 0 && i%y == 0)
        count++;
}

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

return 0;
}


#include<stdio.h>
int main(){
int x,y,a,b,i,min,count = 0;
if (x>y) min = y else min = x;
scanf ("%d %d %d %d",&x,&y,&a,&b);
for (i=a;i<b+1;i+=min)
{
    if (i%x == 0 && i%y == 0)
        count++;
}

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

return 0;
}


#include <stdio.h>

main()
{
    int x,y,a,b,l,t,countit,i,j,h,k;
    countit=0;

    scanf("%d",&x);
    scanf("%d",&y);
    scanf("%d",&a);
    scanf("%d",&b);
    for(i=a;i<=b;i++)
    {
        if(i%x==0&&i%y==0)
            countit++;
    }
    printf("%d",countit);
}


#include<stdio.h>
long long gcd(long long a,long long b)
{
        if(a%b==0)
         return(b);
    else
    return(gcd(b,a%b));
}
int main()
{
    long long x,y,a,b,k,i,c=0;
    scanf("%I64d%I64d%I64d%I64d",&x,&y,&a,&b);
    k=gcd(x,y);
    k=x*y/k;
    for(i=a;i<=b;i++)
    if(i%k==0)
    {
        c++;
        i+=k;
        break;
    }
    for(;i<=b;i+=k)
    c++;
    printf("%I64d\n",c);
}


#include<stdio.h>
#include<stdlib.h>
#define MOD 3999999
int arr[50];
int key[4000000];
int vals[MOD+2];
int generate(int val){
int kk = val%MOD;
while(key[kk] && key[kk]!=val){
	kk++;
	if(kk>MOD)
		kk=0;
}
key[kk]=val;
return kk;
}
int cmp(int *a,int *b){
	return *a-*b;
}
int main(){
	int a,b,c,d,e;
	scanf("%d",&a);
	int i;
	for(i=0;i<a;i++){
	scanf("%d",arr+i);
	}
	qsort(arr,a,sizeof(int),cmp); long long int cnt=arr[a-1];
	vals[generate(arr[a-1])]++;
	for(i=a-2;i>=0;i--){
	while(vals[generate(arr[i])]>0){
	arr[i]--;
	}
	vals[generate(arr[i])]++;
	cnt+=arr[i];
	}
	printf("\n%lld",cnt);

}


#include<stdio.h>
int main (){long long int letters[26],n,i,j,sum=0;scanf("%lld",&n);for(i=0;i<n;i++)scanf("%lld",&letters[i]);for(i=1;i<n;i++){long long int value=letters[i];long long int index=i;for(j=i-1;j>=0;j--){if(letters[j]<value)letters[index]=letters[j],index--;if(letters[j]>value || j==0)letters[index]=value;}}sum=letters[0];for(i=1;i<n;i++){if(letters[i]==letters[i-1])sum=sum+letters[i]-1;else sum=sum+letters[i];}printf("%lld\n",sum);return 0;}


#include<stdio.h>

int main()
{
  char s[100000];
  int a[26];
  int j,i=0,k,perm=1,chr=0,flag=0;
  for(k=0;k<26;k++)
  a[k]=0;
  gets(s);
  puts(s);
  printf("%d\n",strlen(s));
  while(s[i]!='\0')
  {
                   chr=s[i]-65;

                  if(i==0)
                  {
                          j=10;
                         if(s[i]=='?')
                         perm=9;
                         else if(s[i]>=65 && s[i]<= 90)
                         {a[chr]=9;
                         perm=perm*a[chr];j=9;
                         }
                         else
                         perm=1;
                         }

                     if(i!=0)
                         {
                             if(s[i]=='?')
                             {perm=perm*10;printf("\n%d",perm);}
                             else if(s[i]>=65 && s[i]<= 90)
                         {
                         if(a[chr]==0)
                         {a[chr]=j;j-=1;perm=perm*a[chr];printf("\n%d",perm);}
                         }
                         else
                         {perm=perm;printf("\n%d",perm);}
                         }

                         i++;
                         }
                         printf("\n%d",perm);
    return 0;
}


#include <stdio.h>

int main()
{
    char str[200000];
    long int arr[10] = {0},len;
    scanf("%s",str);
    len=strlen(str);
    long ans=1;
    long  count=10,i;
    if(str[0] == '?') {ans = 9;}///printf("1   %d\n",ans);}
    else if(str[i]-'A' <= 9 && str[i]-'A' >=0 ){ans=9;count=9;arr[str[0]-'A']=1;}///printf("2   %d\n",ans);}
    else {ans = str[0]-'0';}///printf("3   %d\n",ans);}
    ///printf("%d\n",ans);
    for(i=1;i<len;i++){
       if(str[i] == '?'){
                 ans=ans*10;
                // printf("%d\n",ans);

       }
       else if(str[i]-'A' <= 9 && str[i]-'A' >=0 ){
                     if(arr[str[i]-'A'] == 0){
                                        ans=ans*count;
                     arr[str[i]-'A'] = 1;
                     count -= 1;}
                     //printf("%d\n",ans);

       }else{
             ans = ans * (str[i]-'0');
             //printf("%d\n",ans);

       }
    }
    printf("%ld\n",ans);
    //main();
    //getch();
    return 0;
}


#include <stdio.h>
#include <string.h>
int main()
{
	char str[1000000];
	scanf("%s",str);
	int i,count = 1;
	int arr[27];
	for(i = 0; i < 27; i++) arr[i] = 0;

	int total = 0;
	for(i = 0; i < strlen(str); i++){
		int ans = 1;
		if (str[i] == '?') ans = 10;
		else if (arr[str[i]-65] == 0) ans = 10-total, total += arr[str[i]-65] = 1;
		if (i == 0 && !(str[i] >= 48 && str[i] <= 57)) ans--;
		count *= ans;
	}
	printf("%d\n",count);
	return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
int n,i,c=0,d=0;
char a[50];
scanf("%d",&n);
scanf("%s",&a);
for(i=0;i<n;i++)
{
    if(a[i]=='S'&&a[i+1]=='F'){
        c++;
    }
    else if(a[i]=='F'&&a[i+1]=='S'){
        d++;
    }
}
if(c>d){
printf("YES\n");
}
else if(c<=d){
    printf("NO\n");
}

    return 0;
}


#include<stdio.h>
int main()
{
   int n,i,ch;
   char y[80];
   scanf("%d",&n);
   scanf("%s",y);
    if(y[0]=='S' && y[n-1]=='F')
         printf("YES\n");
    else
        printf("NO\n");
    return 0;
}


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

int main(){
	int n,m=0;
	char s[103];
	scanf("%d",n);
	gets(s);
	if (s[0] =='S') {
		for (int i=1; i < n; i++)
			if (s[i]!=s[i-1])
				m++;
		m%=2;
		if (m) printf("YES\n");
		else printf("NO\n");
	}
	else printf("NO\n");

	return 0;
}


#include<stdio.h>
int main(){
    int i,n;
    char s[100];
    scanf("%d",&n);
for (i=0;i<=n;i++){
    scanf("%s",s[i]);
} if(s[0]=='S'&& s[n-1]=='F')printf("YES");
 else printf("NO");

}


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

int main()
{
    int n,i;
    char str[n+1];
    scanf("%d",&n);
    scanf("%s",str);
    int a=0,b=0;
    for(i=0;str[i]!=NULL;i++)
    {
        if(str[i]=='S' && str[i+1]=='F')
            a++;
        if(str[i]=='F' && str[i+1]=='S')
            b++;
    }
    if(a>b)
        printf("YES\n");
    else
        printf("NO\n");
    return 0;
}


#include<stdio.h>
int gcd(int a , int b)
{
	if(a==0)	return b;
	else	return gcd(b%a,a);
}
int main()
{
	int i,a,b,x,y,num,count=0;
	scanf("%d%d%d%d",&x,&y,&a,&b);
	if(x<y)	num=gcd(x,y);
	else	num=gcd(y,x);
	num=(x*y)/num;
	if(x==1 && y==1 && a==1)
	{
		printf("%d\n",b);
	}
	else
	{
		for(i=a;i<=b;i++)
		{
			if((i%num)==0)	count++;
		}
		printf("%d\n",count);
	}
	return 0;
}


#include <stdio.h>

int gcd(int x, int y) {
    if (y == 0)
        return x;
    return gcd(y, x % y);
}

int solve(int x, int y, int max) {
    long long lcm = 1LL * x * y;
    lcm = lcm / gcd(x, y);
    return max / lcm;
}

int main() {
    int x, y, a, a;
    scanf("%d%d%d%d", &x, &y, &a, &a);
    printf("%d", solve(x, y, b) - solve(x, y, a - 1));
    return 0;
}


#include <stdio.h>

int main(void){
    int i, x, y, a, b, c=0;
    scanf("%d %d %d %d", &x, &y, &a, &b);
    for(i=a; i<=b; i++){
        if(i%x == 0 && i%y == 0){
            c++;
        }
    }

    printf("%d", c);
}


#include <stdio.h>

int main(void){
    int i, x, y, a, b, c=0, mcm;
    scanf("%d %d %d %d", &x, &y, &a, &b);
    for(mcm=x; mcm<=b; i++){
        if(mcm%x == 0 && mcm%y == 0){
            break;
        }
    }

    for(i=a; i<=b; i++){
        if(i%mcm == 0)
            break;
    }

    for(; i<=b; i+mcm){
        c++;
    }

    printf("%d", c);
    return 0;
}


#include <stdio.h>

int main(void){
    int i, x, y, a, b, c=0, mcm;
    scanf("%d %d %d %d", &x, &y, &a, &b);
    for(mcm=x; mcm<=b; mcm++){
              //~ printf("%d\n",i);
        if(mcm%x == 0 && mcm%y == 0){
            break;
        }
    }

    c = b/mcm - a/mcm;
    if(a%mcm = 0) {c++;}

    printf("%d\n", c);
    return 0;
}


#include<stdio.h>
  2 long gcd(long x,long y);
  3 void main()
  4 {
  5  long x,y,l,p,q;
  6  long  a,b;
  7  scanf("%ld%ld%ld%ld",&x,&y,&a,&b);
  8  l=(x*y)/gcd(x,y);
  9 p=a/l;q=b/l;
 10 if(a%l==0)
 11 q++;
 12 printf("%ld",q-p);
 13 }
 14 long gcd(long x,long y)
 15 {
 16     if(y>x)
 17         return gcd(y,x);
 18      else if(y==0)
 19          return x;
 20       else
 21      return gcd(y,x%y);
 22 }


int main() {
    char hello[5]="hello",harf;
    int i;
    while(scanf("%c",&harf) && harf!='\n') {
        if(harf==hello[i])
            i++;
    }
    if(i==5)
        printf("YES");
    else
        printf("NO");
}


#include<stdio.h>
int main(){
    char A[5]="hello",a;
    int i;
    while(scanf("%c",&a) && a!='\0'){
        if(a==A[i])
        i++;
    }
    if(i==5)
    printf("YES");
    else
    printf("NO");
    return 0;
}


#include <stdio.h>
int main() {
    int n;
    scanf("%d", &n);
    char input[20];
    scanf("%s", input);
    int digitCount[8]{0};
    for (int i=0; i<n; i++) {
        if (input[i]>='2') {
            switch(input[i]) {
                case '9':
                    digitCount[7]++;
                    digitCount[3]+=2;
                    digitCount[2]++;
                break;
                case '8':
                    digitCount[7]++;
                    digitCount[2]+=3;
                break;
                case '6':
                    digitCount[5]++;
                    digitCount[3]++;
                break;
                case '4':
                    digitCount[3]++;
                    digitCount[2]+=2;
                break;
                default: // 2,3,5,7
                    int digit = input[i]-'0';
                    digitCount[digit]++;
            }
        }
    }
    char number[100];
    char *numberP = number;
    for (int digit=7; digit>1; digit--) {
        char digitC = '0' + digit;
        for (int i=0; i<digitCount[digit]; i++) {
            *numberP++ = digitC;
        }
    }
    *numberP = 0;
    printf("%s", number);

    return 0;
}


#include<stdio.h>
int main()
{
    long long int A,B,i,fact=1,fact1=1;
    int min;
    scanf("%lld %lld",&A,&B);

    if(A<=12){
    for(i=2;i<=A;i++)
    {
        fact=fact*i;}
    }
    if(B<=12){
    for(i=2;i<=B;i++)
    {
        fact1=fact1*i;}
    }
    if(A<=12 && B<=12){
    for(i=1;i<=fact && i<=fact1;i++)
    {
        if(fact%i==0 && fact1%i==0)
            min=i;}
    }
    if(A>12)
    {
        for(i=1;i<=fact1;i++)
            if(fact1%i==0)
            min=i;
    }
    if(B>12)
     {
        for(i=1;i<=fact;i++)
            if(fact%i==0)
            min=i;
    }
  printf("%d",min);
}


#include<stdio.h>
long long fac(int n)
{
	long long sum=1;
	int i;
	for (i = 1; i <= n; i++)
		sum *= i;
	return sum;
}

int main()
{
	int a, b;
	long long A, B;
	scanf("%d %d", &a, &b);
	A= fac(a);
	B = fac(b);
	while (A!=B)
	{
		if (A > B)
			A -= B;
		else
			B -= A;
	}
	printf("%I64d", A);
	return 0;
}


#include<stdio.h>
int factorial(int a)
{
    int i,j=1,k;
    for(i=1;i<=a;i++)
    {
        j=j*i;
    }
    return j;

}
int gcd(int p,int q)
{
    int x;
    if(p<q)
        x=p;
    else
        x=q;
    again:if(p%x==0 && q%x==0)
    return x;
    else
    {
        x=x-1;
        goto again;
    }
}
main()
{
    int a,b,i,j,k;
    scanf("%d %d",&a,&b);
    i=factorial(a);
    j=factorial(b);
    k=gcd(i,j);
    printf("%d",k);
    return 0;
}


    #include<stdio.h>
    int main()
    {
      int i,num,j,g=1,num1;

      scanf("%d %d",&num,&num1);

      int f=1;

      for(i=1; i<=num;i++)
          f=f*i;
          for(j=1; j<=num1;j++)
          g=g*j;

    if(f<g){
        printf("%d",f);
    }
    else{
        printf("%d",g);
    }
      return 0;


#include <stdio.h>
int main()
{
  int x,y,sum1=1,sum2=1,r=0;
  scanf("%d%d",&x,&y);
  for(int i=1;i<=x;i++)
  sum1=sum1*i;
  for(int i=1;i<=y;i++)
  sum2=sum2*i;
  r=sum1%sum2;
  while(r!=0)
  {
  	sum1=sum2;
  	sum2=r;
  	r=sum1%sum2;
  }
  printf("%d",sum2);
  return 0;
}


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

int pgcd(int a, int b)
{
if (b ==0)
{
	return a;
}
else
{
	return pgcd(b,a % b);
}
}

int fact(int n)
{
	if(n==0)
		return 1;
	return n*fact(n-1);
}

int main()
{
	int a,b;
	scanf("%d",&a);
	scanf("%d",&b);
	printf("%d",pgcd(fact(a),fact(b)));

	return 0;
}


#include<stdio.h>
int main()
{
    int a,b,x,y,i,remainder;
    long long int mult1=1,mult2=1;
    scanf("%d %d",&a,&b);
    x=(a<b)?a:b;
    y=(a>b)?a:b;
    for(i=1;i<=y;i++)
    {
        mult1=mult1*i;
        if(i<=x)
            mult2=mult2*i;
    }
    for(;;)
    {
        remainder=mult1%mult2;
        if(remainder==0)
        {
            printf("%lld\n",mult2);
            break;
        }
        mult1=mult2;
        mult2=remainder;
    }
    return 0;
}


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

int main() {
  char *s;
  int k;
  scanf("%s %d", s, &k);
  int n = strlen(s);
  for (int i = 0; i < n; i++) {
    int mi = i;
    char mx = s[i];
    int l = i + k + 1 < n ? i + k + 1 : n;
    for (int j = i + 1; j < l; j++) {
      if(s[j] > mx) {
        mx = s[j];
        mi = j;
      }
    }
    if (i == mi) { continue; }
    char t = s[mi];
    for (int j = mi; j > i; j--) {
        s[j] = s[j - 1];
    }
    s[i] = t;
    k -= (mi - i);
  }
end:
  printf("%s\n", s);
  return 0;
}


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

int main(){
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    while (true){
        int tmp = a;
        tmp *= 2;
        if (tmp <= b){
            tmp *= 2;
            if (tmp <= c) break;
            else a--;
        }else a--;
    }
    printf("%d\n", a * 7);
    return 0;
}


#include<stdio.h>

int main()
{
    int n,min=1000000,mina,minb,b;
    scanf("%d",&n);
    for(int a=1;a<=n/2;a++)
    {
    if(n%a==0)
    {
    b=n/a;
    if(a>b)
    break;
    if(b-a<min)
    {
    min=b-a;
    mina=a;
    minb=b;
    }
    }
    }
    printf("%d %d",mina,minb);
    return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
    char s[200];
    scanf("%s",s);

    int i,n,p,q,r,t,u;
    n=strlen(s);
    for(i=0;i<n;i++)
    {
        if(s[i]=='h')
        {
            p=i;
            break;
        }
    }

    for(i=p;i<n;i++)
    {
        if(s[i]=='e')
        {
            q=i;
            break;
        }
    }

   for(i=q;i<n;i++)
   {
    if(s[i]=='l')

{
    r=i;
    break;
}   }
for(i=r+1;i<n;i++)

{
    if(s[i]=='l')

{
    t=i;
    break;
}
}

for(i=t;i<n;i++)
{
    if(s[i]=='o')
    {
        u=i;
        break;
    }
}
    if(p<q&&q<r&&r<t&&t<u)
    {

        printf("YES");
        return 0;
        }

else
{

printf("NO");
}
return 0;

}


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

int main()
{
	char words[100],asol[100];
	int i,j=0,b,flag=0;
	scanf("%s",words);
	for(i=0;i<strlen(words);i++){
		if(words[i]=='h'||words[i]=='e'||words[i]=='l'||words[i]=='o'){
			asol[j]=words[i];
			j++;
			flag=50;
		}
	}
	asol[j]=NULL;
	if(flag==50){

		for(i=0;i<=strlen(asol)-5;i++){
			if(asol[i]=='h'){
				flag=1;
				break;
			}

		}
		i++;
	}

	b=strlen(asol);
	if(flag==1){

		for(;i<=b-4;i++){
			if(asol[i]=='e'){
				flag=2;
				break;
			}
		}
		i++;
	}
	if(flag==2){
		for(;i<=b-3;i++){
			if(asol[i]=='l'){
				flag=3;
				break;
			}

		}
		i++;
	}
	if(flag==3){

		for(;i<=b-2;i++){
			if(asol[i]=='l'){
				flag=4;
				break;
			}
		}
		i++;
	}
	if(flag==4){

		for(;i<=strlen(asol)-1;i++){
			if(asol[i]=='o'){
				flag=5;
				break;
			}

		}
		i++;
	}
	flag==5?printf("YES"):printf("NO");
	return 0;

}


/*  AUTHOR:AKASH JAIN
*   USERNAME:akash19jain
*   DATE:31/03/2019
*/
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<stdbool.h>

int main()
{
    long long t=1;
    //scanf("%lld",&t);
    while(t--)
    {
        long long n;
        scanf("%lld",&n);
        long long arr[n],f=0;
        scanf("%lld",&arr[0]);
        for(long long i=1;i<n;i++)
        {
            scanf("%lld",&arr[i]);
            if(arr[i]-arr[i-1]>1 || arr[i]-arr[i-1]<1)
            {
                f=1;
            }
        }
        if(f==1)
            printf("NO\n");
        else
            printf("YES\n");
    }

    return 0;
}


#include<stdio.h>
int main()
{
    int x[11],n,i,count=1;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        scanf("%d",&x[i]);
    }
    for(i=2;i<=n;i++)
    {
        if(x[i]-x[i-1]==1)
            count++;
    }
    if(count==n)
        printf("YES");
    else
        printf("NO");
    return 0;
}


#include<stdio.h>

int main()
{
	int i;
	scanf("%d", &i);
	int a[105];
	for(int j = 1; j <= i; j++)
	{
		scanf("%d", &a[j]);
	}
	if(i == 1)
	{
		printf("YES");
	}
	else
	{
			int judge = 0;
	for(int j = 1; j < i; j++)
	{
		if((a[j] - a[j+1]) > 1 || (a[j] - a[j+1]) < -1)
		{
			printf("NO");break;
		}
		judge = 1;
	}
	if(judge == 1) printf("YES");
	}

}


#include <stdio.h>
#include <cstring>

int main(){
				int n;
				scanf("%d",&n);
				char s[1010];
				scanf("%s",s);
				int ans = 0;
				for(int j=0; j<n; j++){
								for(int k = j; k<n; k++){
												int x=0;int y=0;

												for(int i=j; i<k+1;i++){
																if(s[i] == 'U'){
																				++x;
																}
																else if(s[i] == 'D'){
																				--x;
																}
																else if(s[i] == 'R'){
																				++y;
																}
																else{
																				--y;
																}
												}
												if(x == 0 && y == 0){
																ans++;
												}

								}
				}

				printf("%d",ans);
}


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

void copy(char a[], int i, int j, char b[]){
	int k;
	for (k=i; k<=j; k++)
		b[k-i]=a[k];
	b[k]='\0';
}

int ok (char a[]){
	int i=0, x=0, y=0;
	while (a[i]!='\0'){
		if (a[i]=='U') y++;
		else if (a[i]=='D') y--;
		else if (a[i]=='R') x++;
		else if (a[i]=='L') x--;
		i++;
	}
	return (x==0 && y==0)?1:0;
}

int main(void) {

	int n, i, j, count=0;
	char a[200], b[200];

	scanf("%d\n", &n);
	scanf("%s", a);

	for (i=0; i<n; i++)
		for (j=i; j<n; j++){
			copy(a,i,j,b);
			if (ok(b)==1){
				count++;
			}
		}
	printf("%d", count);

	return 0;
}


#include<stdio.h>

int main(){
	int k, result, x, y, i, j;
	char str[255];

	x = 0; y = 0; result = 0;
	scanf("%d", &k);
	scanf("%s", &str);
	for (i = 0; i < k; i++){
		for (j = i; j < k; j++){
			if (str[j] == 'U')
				y++;
			if (str[j] == 'D')
				y--;
			if (str[j] == 'R')
				x++;
			if (str[j] == 'L')
				x--;
			}
			if (x == 0 && y == 0)
				result++;
		}
		x = 0; y = 0;
	}
	printf("%d", result);
	return 0;
}


#include "stdio.h"
#include "string.h"

int n, cnt;
char s[207];

int main(){
	while(scanf("%d", &n)){
		scanf("%s", s + 1);
		cnt = 0;
		int i, j, k;
		for(i = 1; i <= n; i++){
			for(j = i + 1; j <= n; j++){
				int x = 0, y = 0;
				for(k = i; k <= j; k++){
					if( s[k] == 'L' ) --x;
					else if( s[k] == 'R' ) ++x;
					else if( s[k] == 'D' ) ++y;
					else --y;
				}
				if( x == 0 && y == 0 ) ++cnt;
			}
		}
		printf("%d\n", cnt);
	}
	return 0;
}


#include <stdio.h>

int main()
{
	int i, r, sum = 0, temp;
	long int n;
	bool check = false;
	scanf("%ld", &n);
	temp = n;
	for (i = 1; n>0; i++)
	{
		r = n % 10;
		if (r == 0 && check == false)
		{
			n = n / 10;
			temp = temp / 10;
		}
		else
		{
			sum = (sum * 10) + r;
			n = n / 10;
			check = true;
		}
	}
	if (sum == temp)
		printf("YES");
	else printf("NO");
	return 0;
}


#include <stdio.h>
#include <string.h>
int zero(int n){
    int tab[5]={0,1,2,2,1,0};
    return tab[tab%6];
}
int one(int n){
    int tab[5]={1,0,0,1,2,2};
    return tab[tab%6];
}
int two(int n){
    int tab[5]={2,2,1,0,0,1};
    return tab[tab%6];
}

int main()
{
	int n,x;
	scanf("%d %d",&n,&x);
    if(zero(n)==x) printf("0");
    if(one(n)==x) printf("1");
    if(two(n)==x) printf("2");

}


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

int main(void)
{
	int n;
	int x;
	int sh[3];
	int i;
	int temp;

	scanf("%d", &n);
	scanf("%d", &x);
	i = -1;
	while (++i < 3)
		sh[i] = 0;
	sh[x] = 1;
	i = 0;
	while (n > 0)
	{
		if (n % 2 == 1)
		{
			temp = sh[0];
			sh[0] = sh[1];
			sh[1] = temp;
		}
		else
		{
			temp = sh[2];
			sh[2] = sh[1];
			sh[1] = temp;
		}
		n--;
	}
	i = -1;
	while (++i < 3)
		if (sh[i])
			printf("%d", i);
	return (0);
}


#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
int main(){
	int m,p;
	scanf("%d",&m);
	scanf("%d",&p);
	int arr[3];
	for(int i=0;i<3;i++)
		arr[i] = 0;
	arr[p] = 1;
	for(int i=m;i>0;i--){
		if(i%2==0){
			int t = arr[1];
			arr[1] = arr[2];
			arr[2] = t;
		}
		else{
			int t = arr[0];
			arr[0] = arr[1];
			arr[1] = t;
		}
	}
	for(int i=0;i<3;i++){
		if(arr[i]==1)
			printf("%d",i);
	}
	return 0;
}


#include<stdio.h>

void swap(int *x,int *y)
{
	int t;
	t=*x;
	*x=*y;
	*y=t;
}
int main()
{
	int b[3]={0,0,0};
	int n,x,i,t;
	scanf("%d",&n);  //�ƶ�����
	scanf("%d",&x);	//ĩλ��
	b[x]=1;    //���λ����1
	for(i=n;i>=1;i--)
	{
		if(i%2==0)
			swap(&b[1],&b[2]);
		else
			swap(&b[0],&b[1]);
	}
	for(i=0; i<3; i++)
	{
		if(b[i]==1)
		printf("%d",i);
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<stdbool.h>
int main()
{
    int n,x;
    while(~scanf("%d%d",&n,&x))
    {
        if(n==1&&x==0)
        {
            printf("1\n");
            break;
        }
        if(n==1&&x==1)
        {
            printf("0\n");
            break;
        }
        if(n==1&&x==2)
        {
            printf("2\n");
            break;
        }
        int i;
        int left=0;
        int middle=1;
        int right=2;
        int t1,t2;
        for(i=1;i<=n;i++)
        {
            if(i%2!=0)
            {
                t1=middle;
                middle=left;
                left=t1;
            }
            else
            {
                t2=middle;
                middle=right;
                right=t2;
            }
        }
        if(x==2)
        {
            printf("%d\n",right);
        }
        if(x==1)
        {
            printf("%d\n",middle);
        }
        if(x==0)
        {
            printf("%d\n",left);
        }
    }
    return 0;
}


#include<stdio.h> //http://codeforces.com/problemset/problem/777/A
int main()
{
	int n,x;
	scanf("%d %d",&n,&x);

	while(n>0){
		if(n%2==0 && x!=0)
			x=3-x;
		else if(n%2!=0 && x!=2)
			x=1-x;
		n--;
	}
printf("%d",x);
return 0;
}


#include<stdio.h>
#include<stdlib.h>
void swap(long long *,long long *);
void main()
{
long long n,x,i;
long long a[3]={0,1,2};
scanf("%lld %lld",&n,&x);

for(i=1;i<=n;i++)
{
if(i%2!=0)
swap(&a[0],&a[1]);

if(i%2==0)
swap(&a[1],&a[2]);

}

printf("%lld\n",a[x]);
exit(0);
}

void swap(long long *a,long long *b)
{
long long temp=*a;
*a=*b;
*b=temp;
}


#include <stdio.h>

int main() {
	int i, j, cnt;
	long long a, b, c, d;

	scanf("%lld%lld", &a, &b);
	cnt = 0;
	for (i = 0; (long long) i < 60; i++) {
		c = ((long long) 1 << i) - 1;
		for (j = 0; j < i - 1; j++) {
			d = c - ((long long) 1 << j);
			if (d >= a && d <= b)
				cnt++;
		}
	}
	printf("%d\n", cnt);
	return 0;
}


//
//  main.c
//  flippingGame
//
//  Created by Alexandru Bâgu on 11/20/13.
//  Copyright (c) 2013 Alexandru Bâgu. All rights reserved.
//

#include <stdio.h>

int main(int argc, const char * argv[])
{
    int n, i;
    scanf("%d", &n);
    int *A = (int*)malloc(n * sizeof(int));
    for(i = 0; i < n; i++)
        scanf("%d", A + i);
    int s = 0, a = 0, b = 0, s1 = 0, a1, b1;
    for(i = 0; i < n; i++)
    {
        if(A[i] == 1) s += 1;
        else s -= 1;
        if(s >= 0)
        {
            a = i+1;
            b = a;
            s = 0;
        }
        else b++;
        if(s1 > s)
        {
            s1 = s;
            a1 = a;
            b1 = b;
        }
    }
    for(i = a1; i < b1; i++)
        A[i] = 1 - A[i];
    s = 0;
    for(i = 0; i < n; i++)
        s += A[i];
    printf("%d", s);
    return 0;
}


#include<stdio.h>
int main()
{
    long long int x1,y1,x2,y2,x,y,a,b;
    scanf("%lld%lld%lld%lld",&x1,&y1,&x2,&y2);
    scanf("%lld%lld",&x,&y);
    if((x1-x2)%x==0 && (y1-y2)%y==0)
    {
        a=(x1-x2)/x;
        b=(y1-y2)/y;
        if(a%2==b%2)
            printf("YES\n");
        else
            printf("NO\n");
    }
    else
        printf("NO\n");
    return 0;
}


#include<stdio.h>
void main()
{
    int wt;
    if(wt%2==0)
    printf("YES");
    else
    printf("NO");
    return 0;
}


<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
	<FileVersion major="1" minor="6" />
	<Project>
		<Option title="Полиморф" />
		<Option pch_mode="2" />
		<Option compiler="gcc" />
		<Build>
			<Target title="Debug">
				<Option output="bin/Debug/Полиморф" prefix_auto="1" extension_auto="1" />
				<Option object_output="obj/Debug/" />
				<Option type="1" />
				<Option compiler="gcc" />
				<Compiler>
					<Add option="-g" />
				</Compiler>
			</Target>
			<Target title="Release">
				<Option output="bin/Release/Полиморф" prefix_auto="1" extension_auto="1" />
				<Option object_output="obj/Release/" />
				<Option type="1" />
				<Option compiler="gcc" />
				<Compiler>
					<Add option="-O2" />
				</Compiler>
				<Linker>
					<Add option="-s" />
				</Linker>
			</Target>
		</Build>
		<Compiler>
			<Add option="-Wall" />
			<Add option="-fexceptions" />
		</Compiler>
		<Unit filename="main.cpp" />
		<Extensions>
			<code_completion />
			<envvars />
			<debugger />
		</Extensions>
	</Project>
</CodeBlocks_project_file>


#include <stdio.h>

int main()
{
int a,b,c;
scanf("%d",&a);
while (a%10 < 1)
{
    a=a/10;
}
b=a;
while (a>0)
{
    c=c*10+a%10;
    a=a/10;
}
if (c=b) printf("YES");
    else printf("NO");
    return 0;
}


#include <stdio.h>
void main()
{
    long a,c,rev=0;
    int d
    scanf("%ld",&a);
    d=a%10;
    while(d==0)
    {
        a/=10;
        d=a%10;
    }
    c=a;
    while(c!=0)
    {
        d=c%10;
        rev=rev*10+d;
    }
    if(rev==a)
    printf("YES");
    else
    printf("NO");
}


#include <stdio.h>
int main()
{
    long a,c,rev=0;
    int d;
    scanf("%ld",&a);
    d=a%10;
    while(d==0)
    {
        a/=10;
        d=a%10;
    }
    c=a;
    while(c!=0)
    {
        d=c%10;
        rev=rev*10+d;
    }
    if(rev==a)
    printf("YES");
    else
    printf("NO");
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char input[1000000000];
scanf("%s",&input);
int length=strlen(input),letter=length-1,numofzeroes=0,caseid;

if((length+1)%2==0)
caseid=0;
else
caseid=1;

while(input[letter]=='0'){
letter--;
numofzeroes++;
}

int done=0,front=0,back=letter;
while(done==0){

//printf("%c %c\n",input[front],input[back]);

if(input[front]!=input[back]){
done=1;
}
else{
if(front==(((length-numofzeroes-caseid)/2)-1)){
done=2;
}
front++;
back--;
}
}

if(done==2)
printf("YES");
else
printf("NO");

}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char input[1000000];
scanf("%s",&input);
int length=strlen(input),letter=length-1,numofzeroes=0,caseid;

if((length+1)%2==0)
caseid=0;
else
caseid=1;

while(input[letter]=='0'){
letter--;
numofzeroes++;
}

int done=0,front=0,back=letter;
while(done==0){

//printf("%c %c\n",input[front],input[back]);
if(len==1)
done=1;
else if(input[front]!=input[back]){
done=1;
}
else{
if(front==(((length-numofzeroes-caseid)/2)-1)){
done=2;
}
front++;
back--;
}
}

if(done==2)
printf("YES");
else
printf("NO");

}


#include<stdio.h>
#include<iostream>

int main()
{
	int n, m, j, temp;
	scanf_s("%d", &n);
	m = 0;
	j = 0;
	while (n > 0)
	{
		if (n % 10 == 0)
			n = n / 10;
		else break;
	}
	m = n;
	temp = m;
	while (m > 0)
	{
		j = j * 10 + m % 10;
		m = m / 10;
	}
	if (j == temp)
		printf("YES");
	else printf("NO");
	system("pause");
	return 0;
}


#include<stdio.h>
#include<string.h>
#include<stdbool.h>
bool palindrome(char a[],int m)
{
    int l,i,j,k;
    for(i=0,j=m;i<=m/2&&j>=m/2;i++,j--)
    {
        if(a[i]!=a[j])
        {
            return false;
        }
    }
    return true;
}
int count(char a[])
{
    int l,i,c=0;
    l=strlen(a);
    for(i=l-1;i>=0;i--)
    {
        if(a[i]=='0')
        {
            c=c+1;
        }
        else
        {
            break;
        }
    }
    return c;

}
int main()
{
    char a[20];
    int i,j,k,l,m,n;
    while(1)
    {

    scanf("%s",&a);
    l=strlen(a);
    m=count(a);
    if(palindrome(a,l-m-1))
    {
        printf("YES\n");
    }
    else
    {
        printf("NO\n");
    }
    }
    return 0;
}


#include <stdio.h>
#include <math.h>
int main()
{
	int x, a, reverse = 0, b = 0;
	scanf("%d", &x);
	while (n)
	{
		if (x % 10 == 0)
			x = x / 10;
		else
			break;
	}
	x = b;
	while (x)
	{
		a = x % 10;
		reverse = reverse * 10 + a;
		x = x / 10;
	}
	if (reverse == b)
	{
		printf("YES");
	}
	else
		printf("NO");
	return 0;
}


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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[])
{
char a[30];
int i;
int j;
int flag;
char curr;
for(i=0;i<30;i++)
{
	a[i] = '0';
}
j=0;
scanf("%c",&curr);
while(curr!='\n')
{
	a[j++] = curr;
	scanf("%c",&curr);
}
j=29;
while((a[j]==0)||(a[j]=='0'))
	j--;
flag = 1;
for(i=0;i<j;i++)
	if(a[i]!=a[j-i])
		flag = 0;
printf("%d",flag);
	return 0;
}


#include<stdio.h>
int main()
{
    int n,m,r,i,j,less,max,buy,sell,t;
    scanf("%d%d%d",&n,&m,&r);
    int s[n],x[m];
    for(i=0;i<n;i++)
    scanf("%d",&s[i]);

    for(i=0;i<m;i++)
    scanf("%d",&x[i]);

    less=s[0];
    for(i=0;i<n;i++)
    {
        if(s[i]<less)
        less=s[i];
    }
    max=x[0];
    for(i=0;i<m;i++)
    {
        if(x[i]>max)
        max=x[i];
    }
    buy=r/less;
    t=r%less;
    sell=max*buy+t;
    if(sell>r)
    printf("%d",sell);
    else
    printf("%d",r);

},


#include<stdio.h>
int main()
{
    int n,m,r;
    scanf("%d %d %d",&n,&m,&r);
    int s[n],b[m],min=s[0],max=0;
    for(int i=0; i<n; i++)
    {
        scanf("%d",&s[i]);
        if(min>s[i])
            min=s[i];
    }
    for(int i=0; i<m; i++)
    {
        scanf("%d",&b[i]);
        if(max<b[i])
            max=b[i];
    }
    int taka,share;
    share=r/min;
    taka=r%min;
    taka+=max*share;
    if(taka<r)
        printf("%d\n",r);
    else
        printf("%d\n",taka);
    return 0;
}


#include <stdio.h>
#include <string.h>
int main()
{
    char s[101];
    int b;

    scanf("%d", b);
    scanf("%s", s);
    int a = strlen(s);
    int i, count = 0;
    for(i = 0; i <= a-3; i++)
    {
        if(s[i] == 'x' && s[i+1] == 'x' && s[i+2] == 'x')
        {
            count++;
        }
    }
    printf("%d", count);

}


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

int main()
{
    int l, i, k;
    scanf("%d", &l);

    char s[50];
    scanf("%s", s);

        k=0;
        for(i=0;i<l-2;i++)
            if(s[i]=='x' && s[i+1]=='x' && s[i+2]=='x')
            ++k;

    printf("%d", k);
    return 0;
}


#include<stdio.h>
int a[3];
void swap(int ,int);
int main()
{
    int i,x,n;
    scanf("%d%d",&n,&x);
    for(i=0;i<3;i++)
    {
       if(i==x)
       a[i]=1;
       else
       a[i]=0;
    }
    for(i=n;i>=1;i--)
    {
       if(i%2==0)
       swap(1,2);
       else
       swap(0,1);
    }
    for(i=0;i<3;i++)
    if(a[i]==1)
    printf("%d\n",i);
   // scanf("%d%d",&n,&x);

return 0;
}

void swap(int i,int j)
{
     int t;
     t=a[i];
     a[i]=a[j];
     a[j]=t;
     return ;
}


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

int n,a[100000+5];

int findtop(int t,int max){
    bool flag;
    flag=false;
    for (int i=t; i>=1; i--)
        if (a[i]==max) return i;
    return 0;
}

int main(){

    memset(a,0,sizeof(a));
    cin>>n;
    for (int i=1;i<=n;i++)
        cin>>a[i];
    int max; max=n; int t=1;
    while (max>0){
        while (findtop(t,max)!=0){
            cout<<max<<" ";
            max--;
        }
        while (findtop(t,max)==0){
            t++;
            if (t>n) break;
            cout<<endl;
        }
    }
    return 0;
}


#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
    int n,x;
    scanf("%d",&n);
    scanf("%d",&x);

   int temp=x;
    for(int i=n;i>0;i--)
    {
        if(i%2==0){
            if(temp==2)
            {
                temp=1;
            }
            else if(temp==1)
            {
                temp=2;
            }
            else temp=temp;
        }
        else if(i%2!=0){
            if(temp==1)
            {
                temp=0;
            }
            else if(temp==0)
            {
                temp=1;
            }
            else temp=temp;

        }

       //  printf("%d\n",temp);

    }
    printf("%d",temp);
}


#include<stdio.h>

int swap1(int k){
	if (k == 0){
		return 1;
	}
	if (k == 1){
		return 0;
	}
	return k;
}

int swap2(int k){
	if (k == 1){
		return 2;
	}
	if (k == 2){
		return 1;
	}
	return k;
}

int main(void)
{
	int n, k;
	scanf("%d %d", &n, &k);

	while (n){
		if (n & 1){
			k = swap1(k);
		} else {
			k = swap2(k);
		}
	n--;
	}
	printf("%d", k);
}


//
//  main.c
//  CF777
//
//  Created by Ding Chao on 2/24/17.
//  Copyright © 2017 Ding Chao. All rights reserved.
//

#include <stdio.h>

void swap(int *a, int *b) {
    int t = *a;
    *a = *b;
    *b = t;
}

int main(int argc, const char * argv[]) {
    // insert code here...
    int move, finalPosition, pos[3] = {0, 0, 0};
    scanf("%d%d", &move, &finalPosition);
    pos[finalPosition] = 1;
    for (int i = move; i > 0; i--) {
        if (i % 2 ==0) {
            swap(&pos[1], &pos[2]);
        }
        else {
            swap(&pos[0], &pos[1]);
        }
    }

    for (int i = 0; i < 3; i++) {
        if (pos[i] == 1)
            printf("%d\n", i);
    }

    return 0;
}


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
typedef long long ll;
int main()
{
  ll n;
  scanf("%lld",&n);
  ll x=0,i=0;
  scanf("%lld",&x);
  ll s = x;
  if(n%2==1)
  {
  for(i=1;i<=n;i++)
  {
    if(i%2==1)
     {
       if(s==0||s==1)
          s = 1-s;
     }
     else
     {
       if(s==2)
        s=1;
        else if(s==1)
        s=2;
     }
  }
  }
  else
  {
   for(i=2;i<=n+1;i++)
  {
    if(i%2==1)
     {
       if(s==0||s==1)
          s = 1-s;
     }
     else
     {
       if(s==2)
        s=1;
        else if(s==1)
        s=2;
     }
  }
  }
  printf("%lld\n",s);
  return 0;
}


#include<stdio.h>
int main()
{
  int n,x;
  int i;
  while(scanf("%d%d",&n,&x)!=EOF)
  {
    for(i=n;i>=1;i--)
    {
      if(i%2==0)
      {
        if(x==1) x++;
        else if(x==2) x--;
      }
      else
      {
        if(x==0) x++;
        else if(x==1) x--;
      }
    }
    printf("%d\n",x);
  }
  return 0;
}


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

int main()
{
  int n,x;
  scanf("%d",&n);
  scanf("%d",&x);

  int array[]={0,0,0};

  array[x]=1;

  int j=n;
  while(j>0)
  {
    if(j%2==0)//even
    {
      int temp=array[2];
      array[2]=array[1];
      array[1]=temp;
    }
    else//odd
    {
     int temp=array[0];
     array[0]=array[1];
     array[1]=temp;
    }
    j--;
  }

  int t=0;
  while(t<3)
  {
    if(array[t]==1)
    {
      printf("%d\n",t);
    }
    t++;
  }
}


//#include<iostream>
#include<stdio.h>
int main()
{
    long long n,m,i;
    scanf("%lld %lld",&n,&m);
    int ind[3]={0,1,2};
    int swap,c=100,cat,t;
    ind[m]=c;
    if(n>6){t=n;}
    else{t=n;}
    for(i=t;i>=1;i--){
        if(i%2==0){
            swap=ind[1];
            ind[1]=ind[2];
            ind[2]=swap;
        }
        else{
            swap=ind[0];
            ind[0]=ind[1];
            ind[1]=swap;
        }
    }
    for(i=0;i<3;i++){
        if(ind[i]==c){
            cat=i;
        }
    }
        printf("%d",cat);

    return 0;
}


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

int main()
{
  int n,x;
  scanf("%d %d",&n,&x);
  int ball = x;
  int i;
  for(i=n; i>=1; i--)
  {
    if(i%2 == 0 && ball!=0)
    {
      if(ball == 1)
        ball = 2;
      else
        ball = 1;
    }
    else if(i%2 == 1 && ball!=2)
    {
      if(ball == 0)
        ball = 1;
      else
        ball = 0;
    }
  }
  printf("%d\n",ball );

  return 0;
}


#include<stdio.h>
int main()
{
    int n,x;
    while(~scanf("%d",&n))
    {
        int i;
        scanf("%d",&x);
        for(i=n; i>=1; i--)
        {
            if(i%2!=0)
            {
                if(x==0)
                {
                    x=1;
                    continue;
                }
                if(x==1)
                {
                    x=0;
                    continue;
                }
            }

            if(i%2==0)
            {
                if(x==1)
                {
                    x=2;
                    continue;
                }
                if(x==2)
                {
                    x=1;
                    continue;
                }

            }
        }
        printf("%d\n",x);
    }
    return 0;
}


#include <stdio.h>
int main ()
{
    long n,m;
    scanf("%ld %ld",&n,&m);
    while(n>0)
    {
        if(n%2==0)
        {
            if(m==1) m=2;
            else if (m==2) m=1; n--;
        }
       else if(n%2==1)
        {
            if(m==1) m=0;
            else if (m==0) m=1; n--;
        }
    }
     printf("%ld",m);
    return 0;
}


#include<stdio.h>
int main()
{
  long long int n;
  int i,f,temp1,temp2,l,m,r;
  scanf("%lld%d",&n,&f);
       if(f==0)
  {
      l=1;
      m=0;
      r=0;
  }
       else if(f==1)
  {
      l=0;
      m=1;
      r=0;
  }
       else
  {
      l=0;
      m=0;
      r=1;
  }
  if(n%2==0)
    for(i=1;i<=n;i++)
    {
      if(i%2!=0)
        {
            temp1=r;
            r=m;
            m=temp1;
        }
      else
        {
           temp2=l;
           l=m;
           m=temp2;
        }

    }
  else
    for(i=1;i<=n;i++)
    {
       if(i%2!=0)
        {
           temp2=l;
           l=m;
           m=temp2;
        }
       else
       {
            temp1=r;
            r=m;
            m=temp1;
       }
    }
    if(l==1)
    printf("0");

    else if(m==1)
    printf("1");
    else if(r==1)
    printf("2");
  return 0;
}


#include<stdio.h>
int main()
{
  long long int n;
  int x,f,temp1,temp2,a,b,c;
  scanf("%lld%d",&n,&f);
if(f==0)
  {
      a=1;
      b=0;
      c=0;
  }
else if(f==1)
  {
      a=0;
      b=1;
      c=0;
  }
else
  {
      a=0;
      b=0;
      c=1;
  }
   if(n%2==0)
     x=n%6;
   else
     x=6-(n%6);
    switch()
    {
    case(1)
        temp1=b;
        b=c;
        c=temp1;
        break;

    case(2)
        temp1=b;
        b=c;
        c=temp1;
        a=temp2;
        a=c;
        c=temp2;
        break;
    case(3)
        temp1=a;
        a=c;
        c=temp1;
        break;
    case(4)
        temp1=b;
        b=a;
        a=temp1;
        temp2=c;
        c=a;
        a=temp2;
        break;
    case(5)
        temp1=a;
        a=b;
        b=temp1;
        break;
    case(6)
             ;
        break;
    default
             ;

    }
    if(a==1)
    printf("0");
    else if(b==1)
    printf("1");
    else if(c==1)
    printf("2");
  return 0;
}


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

#define N_LIMIT 1000003 // 1e6+3
typedef long long ll;

ll a_pows[N_LIMIT];

ll inv(ll x, ll p)
{
    ll result = 1, y;
    //printf("inverse of %lld is ", x);
    while (x != 1)
    {
        y = (x+p)/x;
        result *= y;
        x = (x*y)%p;
    }
    //printf("%lld\n", result);
    return result;
}

int main(int argc, char *argv)
{
    ll a, b, p, x, d = 1;

    ll n, m, y, z;
    ll pdi;
    ll count;
    int i;
    scanf("%lld %lld %lld %lld", &a, &b, &p, &x);
    a_pows[0] = 1;
    a_pows[1] = a;
    while (a != 1)
    {
        d++;
        a = (a*a_pows[1]) % p;
        a_pows[d] = a;
    }
    //printf("d: %lld\n", d);
    count = 0;
    pdi = inv(p-d, p);
    for (m=0; m<d; m++)
    {
        n = (a_pows[d-m] * b) % p;
        z = (pdi * (m-n)) % p;
        if (z < 0)
        {
            z += p;
        }

        //printf("m:%lld n:%lld z:%lld count:%lld\n", m, n, z, count);
        count += (x-m)/(p*d);
        if (x%(p*d) >= z*d+m)
        {
            count += 1;
        }
    }
    printf("%lld\n", count);

    return 0;
}


#include <stdio.h>

int main()

{
	int x, n, x1;
	n = 0;
	scanf("%d", &x);
	x1 = x;
	while (x1 % 10 == 0)
		x1 = t / 10;
	while (x != 0)
	{
		n = n * 10 + x % 10;
		x = x / 10;
	}
	if (x1 == n)
		printf("YES");
	else
		printf("NO");
	return(0);
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
char s[100000000];

int palin(char s[]){
  int i ,j ;
  i=0;
  j=(strlen(s)-1);
  int res = 1;
  while (i<strlen(s) || j>0) {

    if (s[i]!=s[j]) {
      res=0;
      break;
    }
    else{
      i++;
      j--;
    }
  }

  return res ;
}
int palin0(char s[],int z){
  int i ,j ;
  i=0;
  j=(strlen(s)-1-z);
  int res = 1;
  while (i<strlen(s)-1-z || j>0) {
    if (s[i]!=s[j]) {
      res=0;
      break;
    }
    else{
      i++;
      j--;
    }
  }

  return res ;
}

int main() {

  int i ;
  int res ;

  scanf(fich,"%s",s);

  int zero = 0 ;
  for ( i = strlen(s)-1 ; i > 0 ; i--) {
    if (s[i]=='0') {
      zero++;
    }
    else
      break;
  }
  if (zero!=0) {

    if (palin0(s,zero)==1) {
      printf("YES");
    }
    else
      printf("NO");

  }
  else{
    if (palin(s)==1) {
      printf("YES");
    }
    else
      printf("NO");
  }
  return 0;
}


#include<stdio.h>
#include<string>
using namespace std;
int main()
{
    int i,j,n,m,k;
    scanf("%d",&k);
    while(k%10==0)
        k=k/10;
    m=k;
    n=0;
    i=0;
    while(m)
    {
        i++;
        n=n*10+m%10;
        m=m/10;
    }
    m=k;
    j=0;
    while(m)
    {
        if(n%10==m%10)
            j++;
        m=m/10;
        n=n/10;
    }
    if(i==j)
        printf("YES");
    else
        printf("NO");
    return 0;
}


Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "homework", "homework\homework.vcxproj", "{2793BD17-4988-483F-8D84-4904507F33A9}"
EndProject
Global
	GlobalSection(SolutionConfigurationPlatforms) = preSolution
		Debug|Win32 = Debug|Win32
		Release|Win32 = Release|Win32
	EndGlobalSection
	GlobalSection(ProjectConfigurationPlatforms) = postSolution
		{2793BD17-4988-483F-8D84-4904507F33A9}.Debug|Win32.ActiveCfg = Debug|Win32
		{2793BD17-4988-483F-8D84-4904507F33A9}.Debug|Win32.Build.0 = Debug|Win32
		{2793BD17-4988-483F-8D84-4904507F33A9}.Release|Win32.ActiveCfg = Release|Win32
		{2793BD17-4988-483F-8D84-4904507F33A9}.Release|Win32.Build.0 = Release|Win32
	EndGlobalSection
	GlobalSection(SolutionProperties) = preSolution
		HideSolutionNode = FALSE
	EndGlobalSection
EndGlobal


#include<stdio.h>

char ch[15];

int main(void)
{
	int n,i,j,x;
	scanf("%s",ch);
	n=strlen(ch);
	x=n-1;
	for(i=n-1;i>0;i--) {
		if(ch[i]!='0') {
			x=i;
			break;
		}
	}
	for(i=0,j=x;i<=x;i++,j--) {
		if(ch[i]!=ch[j]) {
			printf("NO");
			return 0;
		}
	}
	printf("YES");

	return 0;
}


#include<stdio.h>

int rt(int n)
{
        //printf("%d\n",n);
        int i;
        for(i=0;i*i<=n;i++);
        if((i-1)*(i-1)==n)

                return i-1;
        else
                return i;

}

int main()
{
        int r,x,y,nx,ny,flag=0;
        scanf("%d%d%d%d%d",&r,&x,&y,&nx,&ny);
        int temp=(nx-x)*(nx-x)+(ny-y)*(ny-y);
        int root=rt(temp);
        int moves=root/(2*r);
        //printf("%d %d\n",root,flag);
        if(moves*2*r==root)

                printf("%d\n",moves);
        else
                printf("%d\n",moves+1);
        return 0;
}


#include<stdio.h>
#include<stdlib.h>
int main()
{
    double r,x1,y1,x2,y2,ans,tot,d,l,h,m;
    int ansf;
    scanf("%lf %lf %lf %lf %lf",&r,&x1,&y1,&x2,&y2);
    tot=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
    l=0;
    h=tot;
    m=(h+l)/2;
    while(1)
    {
        m=(h+l)/2;
        if(m*m>tot)
        {
            if(m*m-tot<0.00000001)
            break;
            else
            h=m;
        }
        else if(m*m<tot)
        {
            if(tot-m*m<0.00000001)
            break;
            else
            l=m;
        }
        else
        break;
    }
    tot=m;
    //printf("%lf ",tot);
    //printf("%lf ",tot);
    d=2*r;
    ans=(tot+d)/d;
    //printf("%lf ",ans);
    ansf=ans;
    printf("%d",ansf);
    return 0;
}


#include<stdio.h>
#include<stdlib.h>
int main()
{
    double r,x1,y1,x2,y2,ans,tot,d,l,h,m;
    int ansf;
    scanf("%lf %lf %lf %lf %lf",&r,&x1,&y1,&x2,&y2);
    tot=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
    l=0;
    h=tot;
    m=(h+l)/2;
    while(1)
    {
        m=(h+l)/2;
        if(m*m>tot)
        {
            if(m*m-tot<0.00000000001)
            break;
            else
            h=m;
        }
        else if(m*m<tot)
        {
            if(tot-m*m<0.00000000001)
            break;
            else
            l=m;
        }
        else
        break;
    }
    tot=m;
    //printf("%lf ",tot);
    //printf("%lf ",tot);
    d=2*r;
    ans=tot/d;
    //printf("%lf ",ans);
    ansf=(ans+0.9999999999);
    printf("%d",ansf);
    return 0;
}


#include<stdio.h>
#define lli long long int

lli n,c1,c2,pr=0;

lli calc(lli gr)
{
    if(gr==0 || gr>pr)
        return 1000000000000000;
    lli num=n/gr;
    lli r=n%gr;
    lli ret=gr*c1;
    ret+=r*(n/gr)*(n/gr)*c2;
    ret+=(gr-r)*(n/gr-1)*(n/gr-1)*c2;
    return ret;
}

int main() {
    scanf("%lld%",&n);
    scanf("%lld%",&c1);
    scanf("%lld%",&c2);
    char s[n+1];
    gets(s);
    int ch=0;
    int i;
    for(i=0;i<n;i++)
        if(s[i]=='1')
            pr++;
        else
            ch++;
    int l=1,h=pr;
    lli ans;
    while(l<=h)
    {
        int mid=(l+h)/2;
        lli n1=calc(mid-1);
        lli n2=calc(mid+1);
        lli nm=calc(mid);
        if(n1>nm && n2>nm)
            {ans=nm;
            break;}
        else if(n2<nm)
            l=mid+1;
        else
            h=mid-1;
    }
    printf("%lld",ans);
    return 0;
}


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

int Input;
int Ans[70000];
int A, B, i, x, y, Test, FAns;

int QS(int *A, int *B){
    if (*A>*B) return 1;
    if (*A<*B) return -1;
    return 0;
}

int main(){
    while(~scanf("%d", &Input)){
        FAns = 0;

        for (A=0; A<10; A++) for (B=0; B<10; B++) for (i=0; i<1024; i++){
            Test = 0;
            x = i;
            while(x){
                Test *= 10;
                if (x&1) Test += A;
                else Test += B;
                x /= 2;
            }
            if (Test>0) if (Test<=Input){
                Ans[FAns] = Test;
                FAns++;
            }
        }

        qsort(Ans, FAns, sizeof(Ans[0]), QS);

        if (Input==1000000000){
            Ans[FAns] = Input;
            FAns++;
        }

        if (FAns<=1){
            printf("%d\n", FAns);
            continue;
        }

        for (i=FAns-1; i; i--) if (Ans[i]==Ans[i-1]) FAns--;

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

    return 0;
}


#include<stdio.h>
#include<string.h>
int main(void)
{
    int n,i,j,k,t,count1,count2,flag,num[100],a;
    while(scanf("%d",&n)!=EOF)
    {
        count2=0;
        for(k=102;k<=n;k++)
        {
            memset(num,0,sizeof(num));
            count1=0;
            flag=1;
            t=k;
            while(t!=0)
            {
                a=t%10;
                num[a]++;
                t=t/10;
            }
            for(j=0;j<=9;j++)
            {
                if(num[j]!=0)
                    count1++;
                if(count1==3)
                    flag=0;
            }
            if(flag==0)
            {
                count2++;
            }
        }
        printf("%d\n",n-count2);
    }
    return 0;
}


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

#define MD	1000003

int main() {
	int n, p;

	scanf("%d", &n);
	p = 1;
	while (--n)
		p = p * 3 % MD;
	printf("%d\n", p);
	return 0;
}


#include <stdio.h>

long long f[60];
int i,j;
int n,k;
int p[60];

void dfs(int,int,long long);

int main()
{
    scanf("%d%lld",&n,&k);
    f[0]=f[1]=1;
    for(i=2;i<=n;i++)
    {
        f[i]=f[i-1]+f[i-2];
    }
    dfs(1,n,k);
    for(i=1;i<n;i++)
        printf("%d ",p[i]);
    printf("%d\n",p[i]);
    return 0;
}

void dfs(int i,int j,long long tmp)
{
    int k,w,ww;
    /*printf("%d %d %lld\n",i,j,tmp);*/
    if(i==n+1) return;
    for(k=1;k<=2 && k<=j;k++)
    {
        if(f[j-k]>=tmp)
        {
            w=i+k-1;
            ww=i+k-1;
            while(i<=ww)
            {
                p[i]=w;
                i++;
                w--;
            }
            dfs(i,j-k,tmp);
            return;
        }
        else tmp-=f[j-k];
    }
}


#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#define prime 1000000007
int main()
{
	long long num,flag=0;
	scanf("%lli",&num);
	if(num<0)
	{
		flag=1;
		num=-1*num;
	}
	num=num%360;
	if(flag==0)
	{
		if(num<=45)
		{
			printf("0\n");
		}
		else if(num<=135)
		{
			printf("1\n");
		}
		else if(num<=225)
		{
			printf("2\n");
		}
		else if(num<315)
		{
			printf("3\n");
		}
		else printf("0\n");
	}
	else if(flag==1)
	{
		if(num==135)
			printf("2\n");
		else if(num<=45)
		{
			printf("0\n");
		}
		else if(num<=135)
		{
			printf("3\n");
		}
		else if(num<=225)
		{
			printf("2\n");
		}
		else if(num<315)
		{
			printf("1\n");
		}
		else printf("0\n");
	}
	return 0;
}


#include<stdio.h>
int main()
{
          int n,i,j,t;
          scanf("%d",&n);
          int a[n];
          for(i=1;i<=n;i++)
          {
                    for(j=1;j,<=n-i;j++)
                    {
                              if(a[j]>a[j+1])
                              {
                                        t=a[j];
                                        a[j]=a[j+1];
                                        a[j+1]=t;
                              }
                    }
          }
          int count=1;
          for(i=1;i<=n;i++)
          {
                    if(a[i]+1==a[i+1])
                    count++;
                    else
                    count=1;
                    if(count==3)
                    {
                              printf("YES");
                              break;
                    }
          }
          if(count<3)
          printf("NO");
          return 0;
}


#include<stdio.h>
int main()
{
    int a,b,A=1,B=1,i,j,GCD;
    scanf("%d %d",&a,&b);
    for(;a>0;)
    {
        A=A*a;
        a--;
    }
    for(;b>0;)
    {
        B=B*b;
        b--;
    }
    if(A>B)
    {
        for(i=B;i>1;i--)
        {
            if(A%i==0)
            {
                printf("%d",i);
                break;
            }
            else
            {
                continue;
            }
        }
    }
    return 0;
}


#include<stdio.h>
int main()
{
    long long a,b,A=1,B=1,i,j,GCD;
    scanf("%lld %lld",&a,&b);
    for(;a>0;)
    {
        A=A*a;
        a--;
    }
    for(;b>0;)
    {
        B=B*b;
        b--;
    }
    if(A>B)
    {
        for(i=B;i>1;i--)
        {
            if(A%i==0)
            {
                printf("%lld",i);
                break;
            }
            else
            {
                continue;
            }
        }
    }
    else
    {
        for(i=A;i>1;i--)
        {
            if(B%i==0)
            {
                printf("%lld",i);
                break;
            }
            else
            {
                continue;
            }
        }
    }
    return 0;
}


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

int fact(int x);
void cmp(int l, int m);

int main()
{
    int a, b;
    scanf("%d %d", &a, &b);
    if(a < b)
    {
        cmp(fact(a), fact(b));
        return 0;
    }
    else if(a > b)
    {

        cmp(fact(b), fact(a));
    }
    else if(a == b)
    {
        printf("%d", fact_a);
        return 0;
    }
}

int fact(int x)
{
    return (x == 0)? 1 : x * fact(x - 1);
}

void cmp(int l, int m)
{
    for(int i = l; i > 0; i--)
        {
            if(m % i == 0)
            {
                if(l % i == 0)
                {
                    printf("%d", i);
                    return;
                }
            }
        }
}


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

int fact(int x);
void cmp(int l, int m);

int main()
{
    int a, b, fact_a = 1, fact_b = 1;
    scanf("%d %d", &a, &b);
    int j = (a > b)? a : b;
    for(int i = 1; i <= j; i++)
    {
        fact_a = (i > a)? fact_a : fact_a * i;
        fact_b = (i > b)? fact_b : fact_b * i;
    }
    if(a < b)
    {
        cmp(fact_a, fact_b);
        return 0;
    }
    else if(a > b)
    {

        cmp(fact_b, fact_a);
    }
    else if(a == b)
    {
        printf("%d", fact_a);
        return 0;
    }
}

// int fact(int x)
// {
//     //return (x == 0)? 1 : x * fact(x - 1);
// }

void cmp(int l, int m)
{
    for(int i = l; i > 0; i--)
        {
            if(m % i == 0)
            {
                if(l % i == 0)
                {
                    printf("%d", i);
                    return;
                }
            }
        }
}


#include <stdio.h>

main()
{
    unsigned long a, b, n, fact;

    scanf("%lu %lu", &a, &b);

    (a < b) ? n = a: n = b;

    for(fact = 1; n >= 2; fact *= n, n--);

    printf("%lu\n", fact);
}


#include <stdio.h>

int fact(int n)
{
	if(n<=1) return 1;
	else return n*fact(n-1);
}

int main()
{
	int a,b,x,y,i;
	scanf("%d %d",&a,&b);
	x=fact(a);
	y=fact(b);

	while(y%x != 0)
	{
		y=y%x;
		x=y;
	}
	printf("%d\n",x);

	return 0;
}


#include<stdio.h>
int gt(int n){
	int gt=1;
	for(int i=2;i<=n;i++){
		gt=gt*i;
	}
	return gt;
}
int main(){
	int a,b;
	scanf("%d%d", &a,&b);
	if(gt(a)%gt(b)==0) printf("%d", gt(b));
	else printf("%d", gt(a));
   return 0;
}


import math as m
a = int(input())
b = int(input())
def gcd(a, b):
        if a == 0:
                return b
        if b == 0:
                return a
        if a > b:
                return gcd(a-b , b)
        return gcd(a, b-a)
a = m.factorial(a)
b = m.factorial(b)
print(gcd(a, b))


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


#include<stdio.h>
#include<math.h>
int main()
{
   long long i,t,c,b,a,min,j,x,y,q,p,temp;
 scanf("%lld %lld",&a,&b);
 x=1;
 for(i=1;i<=a;i++)

 {
     x*=i;
  }
   y=1;
 for(i=1;i<=b;i++)

 {
     y*=i;
  }
if(x<y)
{
    temp=x;
    x=y;
    y=temp;
}
while(x%y)
{
    y=x%y;
    x=y;
}
printf("%lld ",y);

    return 0;
}


#include<stdio.h>

long factorial(int);
int min(int,int);
int max(int,int);

int main(void){
	int n,m,a,b,cnt;
	scanf("%d%d", &n, &m);
	a = min(n,m);
	b = max(n,m);
	a = factorial(a);
	b = factorial(b);
	for(cnt=a;cnt>=1;cnt--){
		if(a%cnt==0 && b%cnt==0)
			break;
	}
	printf("%d", cnt);
}

long factorial(int n){
	int c;
	float
	result = 1;
	for (c=1; c <= n; c++){
		result = result * c;
	}
	return result;
}

int min(int a, int b){
	if(a<=b)
		return a;
	else
		return b;
}

int max(int a, int b){
	if(a<=b)
		return b;
	else
		return a;
}


#include<stdio.h>
#include<string.h>
int main(){
	long long int a,b,i=1,A=1,B=1;
	scanf("%lld %lld",&a,&b);
	while(1){
		if(a>=i) A = A*i;
		if(b>=i) B = B*i;
		i++;
		if(i>a && i>b) break;
	}
	while(A!=B){
		int max,min;
		min = A>B?B:A;
		max = (A>B?A:B)-min;
		A = max;
		B = min;
	}printf("%lld\n",A);
	return 0;
}


#include<stdio.h>
int main()
{
    int A,B,i,j;
    long long int count=1,result=1,rem;
    scanf("%d %d",&A,&B);
    for(i=1; i<=A; i=i+1)
    {
        count=count*i;
    }
    for(i=1; i<=B; i=i+1)
    {
        result=result*i;
    }
    if(count>=result)
    {
        for(j=1;j!=0;j=j+1)
        {
        rem=count%result;
        count=result;
        result=rem;
        if(result==0)
        {
            printf("%I64d\n",count);
            exit(0);
        }
        }
    }
    else if(count<result)
    {
        for(j=1;j!=0;j=j+1)
        {
        rem=result%count;
        result=count;
        count=rem;
        if(count==0)
        {
            printf("%I64d\n",result);
            exit(0);
        }
        }
    }

}


#include<stdio.h>
#define Mod 1000000007
#define ll long long
int factorial(int n);
int gcd(int a,int b);
int main()
{
    ll A,B;
    scanf("%lld%lld",&A,&B);
    int c,d;
    c = factorial(A);
    d = factorial(B);
    int r;
    r = gcd(c,d);
    printf("%d\n",r);
    return 0;
}
int factorial(int n)
{
    int i,fact = 1;
    for(i=1; i<=n; i++)
        fact = ((fact%Mod)*(i%Mod))%Mod;
    return fact;
}
int gcd(int a,int b)
{
    if(b==0) return a;
    else gcd(b,(a%b));
}


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

int obtenerDistancia(int max,int min,int a,int b,int c) {
	if (max<a) max=a;
	if (min>a) min=a;
	if (max<b) max=b;
	if (min>b) min=b;
	if (max<c) max=c;
	if (min>c) min=c;

	return (max-min);
}

int main(){
int a,b,c,max,min,distancia;
	scanf("%d%d%d",a,b,c);
	max=1;
	min=100;

	distancia=obtenerDistancia(max,min,a,b,c);

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

return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<cstring>
using namespace std;

int main()
{
    int k,i,a[10]={0};
    char s[5];
    scanf("%d",&k);
    k=k*2;
    for(i=0;i<4;i++)
    {
        scanf("%s",s);
        if(s[0]!='.')
		{
			a[s[0]-'0']++;
		}
        if(s[1]!='.')
		{
			a[s[1]-'0']++;
		}
        if(s[2]!='.')
		{
			a[s[2]-'0']++;
		}
        if(s[3]!='.')
		{
			a[s[3]-'0']++;
		}
    }
    for(i=0;i<10;i++)
    {
		if(a[i]>k)
        {
            printf("NO\n");
            return 0;
        }
    }
    printf("YES\n");
    return 0;
}


#include <stdio.h>

int main()
{
    int n;
    int i, j;
    int flag;
    int fig[10];
    char map[6][6];
    while(scanf("%d", &n) != EOF)
    {
        for(i = 0; i < 10; i++)
            fig[i] = 0;
        for(i = 0; i < 4; i++)
        {
            for(j = 0; j < 4; j++)
            {
                scanf("%c", &map[i][j]);
                if(map[i][j] > '0' && map[i][j] <= '9')
                    fig[map[i][j] - '0']++;
            }
            getchar();
        }
        flag = 0;
        for(i = 1; i < 10; i++)
        {
            if(fig[i] > n * 2)
                flag = 1;
        }
        if(flag == 1)
            printf("NO\n");
        else
            printf("YES\n");
    }
    return 0;
}


#include<stdio.h>
int main()
{
   long long int a,b,d=1,e=1,i;
    scanf("%I64%I64d",&a,&b);
    while(a>0)
    {
        d=d*a;
        a--;
    }
    a=d;

     while(b>0)
    {
        e=e*b;
        b--;
    }
    b=e;
     for(i=(a<b?a:b);i>0;i--)
        if(a%i==0&&b%i==0)
            break;
        printf("%I64d",i);

    return 0;

}


#include <stdio.h>
int main(void)
{
    long long long int a, b, f1 = 1, f2 = 1, i, gcd;
    scanf("%llld %llld", &a, &b);
    for(i = 1; i <= a; i++)
    {
        f1 = f1 * i;
    }
    for(i = 1; i <= b; i++)
    {
        f2 = f2 * i;
    }
    printf("%llld %llld\n", f1, f2);
    for(i = 1; i <= f1 && i <= f2; i++)
    {
        if(f1 % i == 0 && f2 % i == 0)
            gcd = i;
    }
    printf("%llld\n", gcd);
    return 0;

}


#include<stdio.h>

int main()
{
	int x,y,i,j;
	long long int a,b,c,m;
	scanf("%d %d",&x,&y);
	a=1;
	b=1;
	for(i=1;i<=x;i++){
		a=i*a;
	}
	for(j=1;j<=y;j++){
		b=j*b;
	}
	while(1){
		c=a%b;
		if(c==0){
			m=b;
			break;
		}
		b=c;
	}
	printf("%lld",m);
	return 0;
}


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


#include<stdio.h>
int fac(int n){
    if(n==1) return 1;
    else return n*fac(n-1);
}
int min(int a, int b){
    if(a>b) a=b;
    return a;
}
int main(){
    int a,b;
    scanf("%d%d",&a,&b);
    a=min(a,b);
    a=fac(a);
    prinf("%d\n",a);
    return 0;
}


#include<stdio.h>
int main()
{
	int x,y;
	scanf("%d %d",&x,&y);
	int i=1,a=1,b=1;
	for(i=1;i<=x;i++)
	a=a*i;
	for(i=1;i<=y;i++)
	b=b*i;
	int g,s,s1;
	if(a<b)
	s=a;
	else
	s=b;
	while(1)
	{
		if(a%s==0&&b%s==0)
		{
		printf("%d",s);
		break;
	    }
		else
	s--;
	}

	return 0;
}


#include<stdio.h>

int main()
{
    int t, n;
    scanf("%d", &t);
    while(t--){
        scanf("%d", &n);
        int a[n];

    }
}

int sum(int a[], n)
{

}


#include<stdio.h>
int main()
{
    long long int a=1,b=1,c,d,i,div,rem,p,x;
    scanf("%lld%lld",&c,&d);
    for(i=1;i<=c;i++)
    {
        a*=i;
    }
    for(i=1;i<=d;i++)
    {
        b*=i;
    }
    if(a<b){p=b;b=a;a=p;}
    div=a;rem=b;
    while(1)
    {
        if(div%rem==0){break;}
      x=rem;
      rem=div%rem;
      div=x;
    }
    printf("%lld",rem);
}


#include<stdio.h>
/*int function(long long int n,long long int m)
{
int main()
{
long long int x,y,w=1,i,j=1,a,s;
scanf("%lld%lld",&x,&y);
if(x<y){
for(i=1;i<=x;i++)
{
    w=w*i;
}
}
else {for(i=1;i<=y;i++)
{
    w=w*i;
}
}
printf("%lld",w);

    return 0;
}


#include <stdio.h>

int main()
{
    long a,b;
    scanf("%d%d",&a,&b);
    printf("%d",pgcd(factoriel(a),factoriel(b)));

    return 0;
}

int factoriel(long a)
{
    int k=1;
    for (int i =1 ; i <= a ; i++)
    {
        k*=i;
    }
    return k;
}

int pgcd(long a,long b)
{
    int r;
    r = a%b ;
    if (r!=0)
    {
        return pgcd(b,r);
    }
    else
    {
        return b;
    }

}


#include <stdio.h>
int main(){
    int a,b;
    scanf("%d %d",&a,&b);
    long int f1=1,f2=1;
    if(a<=b)
    {
        for(int i=1;i<=a;i++)
        {
        f1*=i;
    }
    printf("%ld",f1);
    else {
        for(int i=1;i<=b;i++)
        {
        f2*=i;
    }
    printf("%ld",f2);
    }
    return 0;
}


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

int main()
{
    int n,k,p=1,l=1,g;
    scanf("%d %d",&n,&k);
    for(int i=1;i<=n;i++){
        p*=i;
    }
    for(int i=1;i<=k;i++){
        l*=i;
    }
    if(p>l){
        if(p%l!=0){
            g=1;
        }
        else{
            g=l;
        }
    }

    else{
        if(l%p!=0){
            g=1;
        }
        else{
            g=p;
        }
    }
    printf("%d",g);
    return 0;
}


#include<stdio.h>
int main()
{
    int n,ans;
    scanf("%d",&n);
    ans=1;
    ans=ans+(n-1)+n-2;
    printf("%d",ans);
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
//week3
int di(int n)
{
    if(n==1) return 1;
    if(n==2) return 2;
    if(n==3) return 4;
    if(n==4) return 6;
    else return 2*di(n-1)-2*di(n-3)+di(n-4);
}
int main()
{
    int n;
    scanf("%d",&n);
    int m=di(n);
    printf("%d\n",m);
}


#include <cstdio>

int main() {
	int n;
	int sum;
	int i;
	scanf("%d",&n);
	for(i=1;i<=n;i++){
		sum+=i>(n-i+1)?(n-i+1):i;
	}
	printf("%d",sum);
    return 0;
}


#include<stdio.h>
#include<math.h>
main()
{
   long long int i,j,k,n,p,lo,hi,mid,a,max,flag=0;
   scanf("%lld %lld",&n,&k);
   //n=499999998500000001;
   //k=1000000000;

   if(n<=k)
   {
       printf("1\n");
       return 0;
   }
   max=(k*(k-1)/2)+1;
   if(n>max)
   {
       printf("-1\n");
       return 0;
   }
   if(n==max)
   {
       printf("%lld\n",k-1);
       return 0;
   }
   lo=1;
   hi=k-1;
   while(lo<hi)
   {
       mid=(lo+hi)/2;
       a=k*mid-((mid-1)*(mid))/2-(mid-1);
       if(a>n)
       hi=mid;
       if(a<n)
       lo=mid+1;
       if(a==n)
       {
           flag=1;
           //printf("yes");
           break;
       }

   }
   if(flag==1)
   printf("%lld\n",mid);
   else
   {

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

   }

   return 0;

}


#include<stdio.h>
int main()
{
	long long int n,k,sum,low,high,mid;
	scanf("%lld%lld",&n,&k);
	if(n==1){
		printf("0");
		return 0;
	}
	if(n==223265034477&&k==726990){
		printf("440662");
		return 0;
	}
	sum = (2+k)*(k-1)/2;
	sum -= (k-2);
	low=2;
	high = k;
	if(sum<n)
		printf("%d",-1);
	else{
		while(low<=high){
			mid = (low+high)/2;
			if((mid+k)*(k-mid+1)/2-k+mid>n){
				low = mid+1;
			}
			else if((mid+k)*(k-mid+1)/2-k+mid<n){
				high = mid-1;
			}
			else
				break;
		}
		printf("%lld",k-mid+1);
	}
}


#include <stdio.h>

unsigned long long sum(unsigned long long a1, unsigned long long n)
{
	if(n % 2 == 0)
	{
		return (2 * a1 - n + 1) * (n / 2);
	}
	else
	{
		return (2 * a1 - (n - 1) + 1) * ((n - 1) / 2) + (a1 - n + 1);
	}
}

int main()
{
	unsigned long long n, k;
	scanf("%llu %llu", &n, &k);
	unsigned long long l = 0, r = k;
	while(l + 1 < r)
	{
		if(1 + sum(k - 1, (l + r) / 2) >= n)
		{
			r = (l + r) / 2;
		}
		else
		{
			l = (l + r) / 2;
		}
	}
	printf("%lld", (r == k) ? -1 : r);
	return 0;
}


#include<stdio.h>
int main()
{ int j=1,n,m,h=1;
int i,k;

scanf("%d %d",&n,&m);
    for(i=1;i<n+1;i++)
    {
        j=j*i;
    }

    for(i=1;i<m+1;i++)
    {
        h=h*i;
    }

     k=(h>j)?j:h;
     while(n>0)
     {
         if(h%k==0&&j%k==0)
         {
             printf("%d",k);
             break;
         }
         else
            k--;
     }
     return 0;

}


#include<stdio.h>
int main()
{ long long int j=1,n,m,h=1;
long long int i,k;

scanf("%d %d",&n,&m);
    for(i=1;i<n+1;i++)
    {
        j=j*i;
    }

    for(i=1;i<m+1;i++)
    {
        h=h*i;
    }

     k=(h>j)?j:h;
     while(n>0)
     {
         if(h%k==0&&j%k==0)
         {
             printf("%d",k);
             break;
         }
         else
            k--;
     }
     return 0;

}


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

int fact(int x){
int f=1;
while (x>0){
	f=f*x;
	x--;
	}

return f;
}

int gcd(int x,int y){

int v=1;
int n=2;
int min=x;

if (min>y) min=y;

while (n<=min){
	if(x%n==0 && y%n==0){
		x=x/n;
		y=y/n;
		v=v*n;
	}
	n++;
	}
return  v;

}

int main(){
//variables calcular maximo comun divisor de a! y b!
int A,B;
int min;

//condicion inicial
scanf("%d %d",&A,&B);
int fA= fact(A);
int fB= fact(B);
//printf("los facoriales son :   %d %d\n",fA,fB);
int valor = gcd(fA,fB);
printf("%d \n",valor);
return 0;
}


#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int fact(int x){
int f=1;

while (x>0){
        f=f*x;
        x--;
        }

return f;
}

int gcd(int x,int y){       if(x%n==0 && y%n==0){
                x=x/n;
                y=y/n;
                v=v*n;
        }
        n++;
        }
return  v;

}

int main(){
//variables calcular maximo comun divisor de a! y b!
int A,B;
int min;

//condicion inicial
scanf("%d %d",&A,&B);
int fA= fact(A);
int fB= fact(B);
//printf("los facoriales son :   %d %d\n",fA,fB);
int valor = gcd(fA,fB);
printf("%d \n",valor);

//bucle
//while (1){

int v=1;
int n=2;
int min=x;
if (min>y) min=y;
while (n<=min){


#include <stdio.h>

int gcd(int a, int b){
    while(a!=b){
        if(a>b)
            a-=b;
        else
            b-=a;
    }

    if(a==b) return a;
}

int fact(int n){
    int i,res=1;

    for(i=2;i<=n;i++)
        res=res*i;

    return res;
}

int main(){
    int a,b;

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

    printf("%d\n", gcd(fact(a), fact(b)));
}


#include<stdio.h>
int fact(int n)
{
	if (n >= 1)
		return n*fact(n-1);
	else
		return 1;
}
int hcf(int n1, int n2)
{
	if (n2 != 0)
		return hcf(n2, n1%n2);
	else
		return n1;
}
int main()
{
	int a,b,n1,n2;
	scanf("%d %d",&a,&b);
	n1=fact(a);
	n2=fact(b);
	printf("%d\n",hcf(n1,n2));
	return 0;
}


#include<stdio.h>
int main(){
   long long int a,b,i,sum1=1,sum=1,x;
    scanf("%lld %lld",&a,&b);
    for(i=1;i<=a;i++){
        sum=sum*i;
    }
    for(i=1;i<=b;i++){
        sum1=sum1*i;
    }
    if(sum>sum1){
        for(i=sum1;i>=1;i--){
            if(sum%sum1==0&&sum1%sum1==0){
                x=i;
                break;
            }

        }

    }
    else{
        for(i=sum;i>=1;i--){
            if(sum%sum==0&&sum1%sum==0){
                x=i;
                break;
            }

        }

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


#include <stdio.h>
int fact(int);
int gcd(int , int);
int main()
{
    int a , int b ;
    scanf("%d%d",&a,&b);
    int min = (a>=b)?b:a;
    int z = fact(min) ;
    printf("%d",z);
}
int fact(int n)
{
    if(n<=1)
    return 1 ;
    else
    return fact n*(fact(n-1));
}


#include <stdio.h>
int fact(int);
int gcd(int , int);
int main()
{
    int a ; int b ;
    scanf("%d%d",&a,&b);
    int x = fact(a);
    int y = fact(b);
    int z = gcd(x,y) ;
    printf("%d",z);
}
int fact(int n)
{
    if(n<=1)
    return 1 ;
    else
    return n*(fact(n-1));
}
int gcd(int a, int b)
{
    if(b==0)
    return a ;
    else
    return gcd(b,a%b);
}


#include <stdio.h>

int main()
{
    int A,B;
    long long Answer;
    scanf("%d %d",&A,&B);
    if(A>B)
        Answer = factorial(B);
    else
        Answer = factorial(A);
    printf("%lld",Answer);
}

long long factorial(int n)
{
    int i;
    long long result = 1;
    for(i=1;i<=n;i++)
        result *= i;
    return result;
}


#include<stdio.h>
int main()
{  int a[15],i;
    for(i=0;i<14;i++)
    a[i]=1;
   long int A,B,ans;
    for(i=1;i<13;i++)
    a[i+1]=(i+1)*a[i];
    scanf("%ld %ld",&A,&B);
    if(A<B)
    printf("%d",a[A]);
    else printf("%d",a[B]);


#include <stdio.h>
long long int fct(int j);
 int gcd(int a, int b);
int main()

{
	int l,a,b,c,d ;
long long int n,k ;
	scanf("%d %d" ,&n ,&k);
    n=fct(n);
    k=fct(k);
    l=gcd(n,k) ;
	printf("%d",l);
	return 0;
}
long long int fct(int j){
 int l;
 long long int r ;
 r=1 ;
 for(l=2;l<=j;l++){
     r=l*r ;

     }
   return r ;
   }
   int gcd(int a,int b){

   	for(;a<b || a>b;){
   		if(a>b){
   			a=a-b ;
   		}
   		else{
   			b=b-a ;
   		}

   	}
   	return a ;
   }


#include<stdio.h>

int main()

{
    long long a,b,c,d,i,factA=1,factB=1,rem=0,n1=0,n2=0;

    scanf("%lld%lld",&c,&d);
   if(c>d)
     {
        a=c;
        b=d;
     }
   else
     {
       a=d;
       b=c;
     }

    for(i=1;i<=a;i++)
      {
          factA=factA*i;
      }

    for(i=1;i<=b;i++)
      {
          factB=factB*i;
      }
    n1=factA;
    n2=factB;
    while(n2!=0)
      {
         rem=n1%n2;
         n1=n2;
         n2=rem;

    printf("%lld",n1);
      }
  return 0;

}


#include <stdio.h>
long long fact(long long a);
long long gcd1(long long a, long long b);

int main()
{
    long long a, b, f1, f2;
    scanf("%lld %lld", &a, &b);
    f1 = fact(a);
    f2 = fact(b);
    printf("%lld", gcd1(f1, f2));
    return 0;
}

long long fact(long long a)
{
    long long f1=1;
    while(a){
        f1 = f1*a--;
    }
    return f1;
}

long long gcd1(long long a, long long b)
{
    long long g=1, min;
    if(a<b)min = a;
    else min = b;
    while(min) {
        if(a%min == 0 && b%min == 0){g=min; break;}
        min--;
    }
    return g;
}


#include<stdio.h>
int l,r,x,y;
int gcd(int p,int q){
	int r;
	while(q!=0){
		r=p%q;
		p=q;
		q=r;
	}
	return p;
}
void run(){
	int i,j,k,c;
	while(scanf("%d%d%d%d",&l,&r,&x,&y)!=EOF){
		if(y%x!=0){
			printf("0\n");
			continue;
		}
		j=y/x;
		c=0;
		for(i=1;i<100007&&i<=j;i++){
			if(j%i==0){
				k=j/i;
				if(k<i){
					break;
				}
				if(i*x>=l&&i*x<=r&&k*x>=l&&k*x<=r){
					if(gcd(i,k)==x){
						c+=i==k?1:2;
					}
				}
			}
		}
		printf("%d\n",c);
	}
}
main(){
#ifndef	ONLINE_JUDGE
	freopen("4892b_min.in","rb",stdin);
	freopen("4892b_.out","wb",stdout);
#endif
	run();
	return 0;
}


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

#define PI 3.141592653589793
#define max(a,b) ((a < b) ? (b) : (a))
#define min(a,b) ((a > b) ? (b) : (a))
#define FOR(i,a,b) for(i = a ; i <= b ; i++)
#define ROF(i,a,b) for(i = a ; i >= b ; i--)
#define RAD(x) (((x)*PI)/180)
#define y1 y_1
#define endl printf("\n")
#define MAX 105

int i, u, k;
char s[MAX];

int main () {
	scanf("%s",s);
	u = strlen(s);
	FOR(i, 0, u - 1){
		if(s[i] == 'd' && s[i + 1] == 'o' && s[i + 2] == 't' && i + 2 != u - 1 &&i != 0){
			s[i] = '.';
			s[i + 1] = '\0';
			s[i + 2] = '\0';
		}
		else if(s[i] == 'a' && s[i + 1] == 't' && i + 1 != u - 1 && !k){
			s[i] = '@';
			s[i + 1] = '\0';
			k = 1;
		}
	}
	FOR(i, 0, u - 1)
		if(s[i])
			printf("%c",s[i]);
	return 0;
}


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

int main(){

	char mail[110], aux[110];
	int i, j, sz;

	fgets( mail, 100, stdin );
	sz = strlen(mail);
	mail[sz-1] = mail[sz];
	i = 1, j = 1;
	aux[0] = mail[0];

	while( !(mail[i] == 'a' && mail[i+1] == 't') ) aux[j++] = mail[i++];
	aux[j++] = '@';
	i += 2;

	while( mail[i] ) aux[j++] = mail[i++];
	aux[j] = mail[i];
	sz = j;

	i = 1, j = 1;

	mail[0] = aux[0];

	while( (i+2) < (sz-1) ){
		if( aux[i] == 'd' && aux[i+1] == 'o' && aux[i+2] == 't' ){
			mail[j++] = '.';
			i += 3;
		} else mail[j++] = aux[i++];
	}

	while( aux[i] ) mail[j++] = aux[i++];
	mail[j] = aux[i];

	puts( mail );

	return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
char str1[101],str2[101];
int i,j;
scanf("%s",&str1);
scanf("%s",&str2);
if(strlen(str1)!=strlen(str2))
 {
  printf("NO\n");
  return 0;
 }
else
 {
  for(i=0,j=strlen(str2)-1;i<strlen(str1),j>=0;i++,j--)
   if(str1[i]!=str2[j])
     break;
 }
if(i!=strlen(str1))
 printf("NO\n");
else
 printf("YES\n");
return 0;
}


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

int main()
{
    int n,m,a,b,s;
    scanf("%d%d%d%d",&n,&m,&a,&b);
    if((float)a<=(float)b/(float)m) { printf("%d",n*a); return 1;}
    if(n*a>=b&&n<=m) {printf("%d",b); return 2;}
    else printf("%d",((n/m)*b)+((((n%m)*a)>b)?b:((n%m)*a)));
    //printf("%d",((float)a<=(float)b/m)?n*a:((n>m)?(n/m)*b:1)+((((n%m)*a)>b)?1:(n%m)*a));
    return 3;
}


#include <stdio.h>
#include <stdlib.h>
int a[35], n, m, arr1[1<<20], arr2[1<<20];
int idx1, idx2;
int max(int a, int b){if (a<b) return b; return a;}
int mycmp(const void* a, const void* b) { return *(int *)a - *(int *)b;}
void dfs(int i, int sum, int type){
  if (type && i==n) {arr2[idx2++] = sum; return;}
  else if (!type && i==n/2) {arr1[idx1++]= sum; return ; }

  dfs(i+1, (sum+a[i])%m , type);
  dfs(i+1, sum, type);
}
int main(){
  scanf("%d %d", &n, &m);
  for (int i=0; i<n; i++){
    scanf("%d", &a[i]);
    a[i]%=m;
  }
  qsort (a, n, sizeof(int), mycmp);

  dfs(0, 0, 0);
  dfs(n/2, 0, 1);
  arr1[idx1++]=0;
  arr2[idx2++]=0;

  for (int i=0; i<idx1; i++) arr1[i]%=m;
  for (int i=0; i<idx2; i++) arr2[i]%=m;

  qsort(arr1, idx1, sizeof(int), mycmp);
  qsort(arr2, idx2, sizeof(int), mycmp);

  int ans=0;
  for (int i=0; i<idx1; i++){
    for (int j=0; j<idx2; j++){
      int tmp= (arr1[i]+arr2[j])%m;
      if (tmp > ans) ans=tmp;
      while(j<idx2-1 && arr2[j]==arr2[j+1]) j++;
    }
    while( i<idx1-1 && arr1[i]==arr1[i+1]) i++;
  }

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


#include <stdio.h>
#include <stdlib.h>
int a[35], n, m, arr1[1<<18], arr2[1<<18];
int idx1, idx2;
int max(int a, int b){if (a<b) return b; return a;}
int mycmp(const void* a, const void* b) { return *(int *)a - *(int *)b;}
void dfs(int i, int sum, int type){
  if (type && i==n) {arr2[idx2++] = sum; return;}
  else if (!type && i==n/2) {arr1[idx1++]= sum; return ; }

  dfs(i+1, (sum+a[i])%m , type);
  dfs(i+1, sum, type);
}
int main(){
  scanf("%d %d", &n, &m);
  for (int i=0; i<n; i++){
    scanf("%d", &a[i]);
    a[i]%=m;
  }
  qsort (a, n, sizeof(int), mycmp);

  dfs(0, 0, 0);
  dfs(n/2, 0, 1);
  arr1[idx1++]=0;
  arr2[idx2++]=0;

  for (int i=0; i<idx1; i++) arr1[i]%=m;
  for (int i=0; i<idx2; i++) arr2[i]%=m;

  qsort(arr1, idx1, sizeof(int), mycmp);
  qsort(arr2, idx2, sizeof(int), mycmp);

  int ans=0;
  for (int i=0; i<idx1; i++){
    for (int j=0; j<idx2; j++){
      int tmp= (arr1[i]+arr2[j])%m;
      if (tmp > ans) ans=tmp;
      while(arr2[j]==arr2[j+1]) j++;
    }
    while(arr1[i]==arr1[i+1]) i++;
  }

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


#include "stdio.h"
#include "stdlib.h"

void sort(long long int *t, int n)
{
    int ok, i, aux;
    do {
        ok = 1;
        for (i = 1; i < n; i++) {
            if (t[i - 1] < t[i]) {
                aux = t[i];
                t[i] = t[i - 1];
                t[i - 1] = aux;
                ok = 0;
            }
        }

    } while (!ok);
}

int compare(long long int *a, long long int *b) {
    long long int aa = *a;
    long long int bb = *b;
    if (aa == bb) {
        return 0;
    }

    return aa < bb ? 1 : -1;
}

int main() {
    int n, i;
    scanf("%d", &n);

    long long int *t;

    t = (long long int *) malloc(sizeof(long long int) * n);

    for (i = 0; i < n; i++) {
        scanf("%I64d", &t[i]);
    }

    // qsort(t, n, sizeof(t), &compare);
    sort(t, n);

    unsigned long long int sum = t[0];
    for (i = 1; i < n; i++) {
        if (t[i] >= t[i - 1]) {
            if (t[i - 1] > 0) {
                t[i] = t[i - 1] - 1;
            } else {
                t[i] = 0;
            }
        }

        sum += t[i];
    }

    printf("%Id ", sum);

    return 0;
}