Amr bought a new video game "Guess Your Way Out!". The goal of the game is to find an exit from the maze that looks like a perfect binary tree of height h. The player is initially standing at the root of the tree and the exit from the tree is located at some leaf node. Let's index all the leaf nodes from the left to the right from 1 to 2h. The exit is located at some node n where 1 ≤ n ≤ 2h, the player doesn't know where the exit is so he has to guess his way out!Amr follows simple algorithm to choose the path. Let's consider infinite command string "LRLRLRLRL..." (consisting of alternating characters 'L' and 'R'). Amr sequentially executes the characters of the string using following rules:  Character 'L' means "go to the left child of the current node";  Character 'R' means "go to the right child of the current node";  If the destination node is already visited, Amr skips current command, otherwise he moves to the destination node;  If Amr skipped two consecutive commands, he goes back to the parent of the current node before executing next command;  If he reached a leaf node that is not the exit, he returns to the parent of the current node;  If he reaches an exit, the game is finished. Now Amr wonders, if he follows this algorithm, how many nodes he is going to visit before reaching the exit?

Input consists of two integers h, n (1 ≤ h ≤ 50, 1 ≤ n ≤ 2h).

Output a single integer representing the number of nodes (excluding the exit node) Amr is going to visit before reaching the exit by following this algorithm.

NoteA perfect binary tree of height h is a binary tree consisting of h + 1 levels. Level 0 consists of a single node called root, level h consists of 2h nodes called leaves. Each node that is not a leaf has exactly two children, left and right one. Following picture illustrates the sample test number 3. Nodes are labeled according to the order of visit.

#include <stdio.h>

int main(int argc, char const *argv[]){
	int h,i=0;
	long long n, acc=0, l;
	scanf("%d%I64d", &h, &n);
	l = 1ll << h;
	while (h--){
		i = !i;
		l >>= 1;
		if ((i && n<=l) || (!i && n>l)){
			++acc;
		}
		else{
			acc += 1ll << (h+1);
			i = !i;
		}
		if (n>l) n -= l;
	}
	printf("%I64d\n", acc);
	return 0;
}

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

int ancestor(long long a, long long b) {
	while (b > a)
		b >>= 1;
	return b == a;
}

int main() {
	int h, c;
	long long n, x, ans;

	scanf("%d%lld", &h, &n);
	n += (1LL << h) - 1;
	x = 1, ans = h;
	c = 0;
	while (x != n)
		if (ancestor(x, n)) {
			x = x << 1 ^ c; c ^= 1;
			h--;
		} else {
			ans += (1LL << h + 1) - 1;
			x >>= 1;
			h++;
		}
	printf("%lld\n", ans);
	return 0;
}

#include <stdio.h>

int main()
{
    char goL = 1;
    int H;
    long long N, M, Ans = 0;
    scanf("%d %I64d", &H, &N);
    M = 1LL << H;
    while(H) {
        if(goL) {
            if(N > (M >> 1)) {
                Ans += M;
                N -= M >> 1;
            } else {
                ++Ans;
                goL = 0;
            }
        } else {
            if(N <= (M >> 1)) {
                Ans += M;
            } else {
                ++Ans;
                goL = 1;
                N -= M >> 1;
            }
        }
        --H;
        M = 1LL << H;
    }
    printf("%I64d\n", Ans);
    return 0;
}

#include <stdio.h>

long long int potencia (long long int exp){
    long long int i, resultado = 1;
    for(i = 0; i < exp; i++)
        resultado *= 2;
    return resultado;
}

int main(){
    long long int comeco, quantidade = 0, i, h, n, atual, l;
    char passos[100];
    for(i = 0; i < 55; i++)
        if(i%2 == 0)
            passos[i] = 'L';
        else
            passos[i] = 'R';
    scanf("%I64d %I64d", &h, &n);
    i = 0;
    comeco = atual = 1;
    l = h;
    while(n != atual - potencia(l) + 1){
        if(passos[i] == 'L'){
            if(n <= comeco + potencia(h)/2 - 1){
                quantidade += 1;
                h--;
                atual = 2*atual;
                i++;
            }else{
                quantidade += potencia(h);
                h--;
                comeco += potencia(h);
                atual = 2*atual + 1;
            }
        }else{
            if(n <= comeco + potencia(h)/2 - 1){
                quantidade += potencia(h);
                h--;
                atual = 2*atual;
            }else{
                quantidade += 1;
                h--;
                comeco += potencia(h);
                atual = 2*atual + 1;
                i++;
            }
        }
    }
    printf("%I64d\n", quantidade);
    return 0;
}

#include <stdio.h>
long long arr[65];
int main(void) {
    long long i,a[65],h,j,k,m,n,x,y,z,next,ans;
    scanf("%lld %lld",&h,&n);
    arr[0]=1;
    for(i=1;i<62;++i) arr[i]=arr[i-1]*2;
    n=arr[h]-1+n;
    m=n;
    for(i=h-1;i>=0;--i){
        x=m/2;
        if(2*x==m) a[i]=0;
        else a[i]=1;
        m=m/2;
    //  printf("%lld %lld\n",m,a[i]);
    }
    z=0;
    next=0;
    ans=1;
    while(z<h){
    //  printf("%lld %lld\n",next,arr[h-z]-1);
        if(next!=a[z]) ans+=arr[h-z]-1;
         ans++;
        next=1-a[z];
        z++;
    }
    printf("%lld\n",ans-1);
    return 0;
}

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

unsigned long long calculateNodesOfTree(int h){
    unsigned long long count = 0;
    for(int i = 0; i <= h ;i++){
        count = count + pow(2,i);
    }
    return count;
}

unsigned long long countNodes(int h, unsigned long long n){
    unsigned long long count = 0;
    int h0 = h;
    while(1){
        h--;
        if(h == -1) break;
        unsigned long long k1 = (double)n/2;

        unsigned long long k2;                 //parent node
        if(n % 2 == 0)  k2 = n/2;
        else    k2 = n/2 + 1;

        count++;
        if(k2 == 1 && h == 0){            //root node
            if(k1 == k2){       //node is Right of root node
                count = count + calculateNodesOfTree(h0-h-1);
            }
        }else{
            if(k2 % 2 == 0){        //even number - LR
                if(k1 == k2){        //node is Right of parent node
                    count = count + calculateNodesOfTree(h0-h-1);
                }
            }else if(k2 % 2 == 1){  //odd number - RL
                if(k1 < k2){        //node is Left of parent node
                    count = count +  calculateNodesOfTree(h0-h-1);
                }
            }
        }
        n = k2;
    }
    return count;
}

int main() {
	int h;
	unsigned long long n;
	scanf("%d %I64d", &h,&n);
	printf("%I64d", countNodes(h,n));

    return 0;
}

#include<stdio.h>
#include<math.h>
long long int ans=0;
void compute(char cmd,long long int n,long long int h,long long int power)
{
	if(h==0)
		return ;
	long long int l=power;
	long long int m=l/2;
	if(cmd=='L' && n>m)
	{
		ans=ans+l;
		long long int o=n%m;
		if(o==0)
			o=m;

		compute('L',o,h-1,l/2);
	}
	else if(cmd=='L' && n<=m)
	{
		ans=ans+1;
		compute('R',n,h-1,l/2);
	}
	else if(cmd=='R' && n<=m)
	{
		ans=ans+l;
		compute('R',n,h-1,l/2);
	}
	else if(cmd=='R' && n>m)
	{

		long long int o=n%m;
		if(o==0)
			o=m;
		ans=ans+1;
		compute('L',o,h-1,l/2);
	}

}
int main()
{
	long long int h,n;
	scanf("%lld %lld",&h,&n);
	long long int power=pow(2,h);
	compute('L',n,h,power);
	printf("%lld\n",ans);
	return 0;
}

#include<stdio.h>
int main()
{
	long long int n,sum=0;
	long long int h;
	scanf("%I64d%I64d",&h,&n);
	int i=0;
	long long int l,r,m1,m2;
	l=1;
	r=1ll<<h;
	int fanxiang=0;
	for(i=0;i<h;i++)
	{
		int s=0;
		m1=(l+r)/2;
		m2=(l+r)/2+1;
		if(fanxiang==0&&n<=m1&&n>=l)
		{
			sum+=1;
			fanxiang=1;
			s=1;
		}
		else if(fanxiang==0&&n>=m2&&n<=r)
		{
			sum+=(1ll<<(h-i));
			s=2;
		}
		else if(fanxiang==1&&n<=m1)
		{
			sum+=(1ll<<(h-i));
			s=1;
		}
		else if(fanxiang==1&&n>=m2&&n<=r)
		{
			sum+=1;
			fanxiang=0;
			s=2;
		}
		if(s==1)
		{
			r=m1;
		}
		if(s==2)
		{
			l=m2;
		}

	}
	printf("%I64d\n",sum);
	return 0;

}

#include<stdio.h>
long long int a[51],ans;
void count(long long int h,long long int n,long long int flag){
	if(h==0)
		return ;
	if(flag==0){
		if(n <= a[h]/2){
			ans++;
//			printf("%d ",ans);
			count(h-1,n,1);
		}
		else{
			ans = ans + a[h];
//			printf("%d ",ans);
			count(h-1,n-a[h]/2,0);
		}
	}
	else{
		if(n > a[h]/2){
			ans++;
//			printf("%d ",ans);
			count(h-1,n-a[h]/2,0);
		}
		else{
			ans = ans + a[h];
//			printf("%d ",ans);
			count(h-1,n,1);
		}
	}
	return ;
}
int main(){
	long long int h,i,j,k,l,n,m;
	ans=0;
	a[0] = 1;
	for(i=1;i<=50;i++)
		a[i]=a[i-1]*2;
//	for(i=0;i<10;i++)
//		printf("%d ",a[i]);
	scanf("%lld%lld",&h,&n);
	count(h,n,0);
	printf("%lld\n",ans);
	return 0;
}

#include <stdio.h>

long long answer(long long n,int h)
{
	int i,j,check;
	long long total = 0,temp = 0,temp1 = 0;
	for(i = h;i > 0;i--)
	{
		temp = 1;
		if(i == 1 && (n % 2) == 0)
		{
			for(j = 0;j < h - i + 1;j++) temp *= 2;
			total += temp;
			continue;
		}
		else if(i == 1 && (n % 2) != 0)
		{
			total++;
			continue;
		}
		if((n % 2 == 0 && (n / 2) % 2 == 0) || (n % 2 != 0 && ((n + 1) / 2) % 2 != 0))
		{
			if(i == h)
			{
				total += 2;
				temp1 = n;
				n /= 2;
				if(temp1 % 2 > 0)
				n++;
				continue;
			}
			for(j = 0;j < h - i + 1;j++) temp *= 2;
			total += temp;
		}
		else
			total++;
		temp1 = n;
		n /= 2;
		if(temp1 % 2 > 0)
			n++;
	}
	return total;
}

int main()
{
	int h,i;
	long long total = 0,n;
	scanf("%d%lld",&h,&n);
	printf("%lld",answer(n,h));
	return 0;
}

#include<stdio.h>
#include<math.h>
typedef long long ll;
int main()
{
    int h;
    ll n;
    while(scanf("%d%I64d",&h,&n) != EOF)
    {
        ll a,b,s = 0;
        for( ; h > 0; h--)
        {
            a = pow(2,h);
            if(a/2 >= n)
            {
                s++;
                n = a/2-n+1;
                continue;
            }
            else if (a/2 < n)
            {
                s += a;
                n -= a/2;
                continue;
            }

        }

        printf("%I64d\n",s);
    }
    return 0;
}

#include<stdio.h>
typedef long long ll;
ll array[55];
int check(ll dest,ll cur)
{
      if(cur==dest)
      return 1;
      while(cur>=1)
      {
           cur  = cur/2;
           if(cur==dest)
           return 1;
      }
      return 0;
}
int main()
{
      int i;
      array[0] = 1;
      for(i=1;i<55;i++)
      array[i]= (array[i-1]+1)*2-1;
      ll h,n;
      scanf("%lld%lld",&h,&n);
      n = n + array[h-1];
      ll count = 1;
      ll index = 1,tmp,ht = h-1;
      int flag=0;
      while(index!=n)
      {
            if(flag==0)
            tmp=2*index;
            else if(flag==1)
            tmp = index*2+1;
            if(check(tmp,n))
            {
                  count+=1;
                  index = index*2+flag;
                  flag = (flag+1)%2;
                  ht--;
            }
            else
            {
                   count+=array[ht];
                   flag = (flag+1)%2;

            }

      }
      count--;
      printf("%lld\n",count);
}

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

long long getpath(long long pos, long long h, long long n, int indi, int *found)
{
    long long x = 0;
    if (h == 0 && n == pos) { *found = 1; return 0; }
    else if (h == 0) return 1;
    long long tmp = n;

    tmp = tmp>>(h);
    long long y = 1;
    if (tmp != pos) return (y<<(h + 1)) - 1;

    x = getpath(pos * 2 + indi, h - 1, n, !indi, found);
    if (!*found) x += getpath(pos * 2 + !indi, h - 1, n, indi, found);

    return x + 1;

}
int main(int argc, char **argv)
{
    long long h, n;
    scanf("%I64d%I64d", &h, &n);
    n--;
    long long x = 0;
    int found = 0;
    x = getpath(0, h, n, 0, &found);
    printf("%I64d\n", x);
    //system("pause");
    return 0;
}

// for level 2, it is {1, 2}
// for level 3, it is {3, 2, 5, 6}
// FIXED: wrong result for (39 457181784666) =>
// WRONG ANSWER, where the correct answer is
// 830699159852

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

typedef uint64_t ull;

ull f(ull h, ull k)
// number of nodes visited before exiting at k-th leaf nodes
{
	//printf("calling [%"PRIu64 ", %"PRIu64"]\n", h, k);
	if(h == 1) return k;
	if( k > (1ULL<<(h - 1)))
		return (1ULL<<h) + f(h - 1, k - (1ULL<<(h - 1)));
	else return 1 + f(h - 1, (1ULL<<(h - 1)) - k + 1);
}
/*
void test()
{
	ull one = 1ULL;
	for(int i = 0; i < 63; i++){
		one = 1ULL<<i;
		printf("one is %" PRIu64 "\n", one);
	}
}
*/
int main()
{
	ull h, n;
	//test();
	//while(1){
		scanf("%"SCNu64" %"SCNu64, &h, &n);
		printf("%" PRIu64, f(h, n));
	//}
	return 0;
}

#include<stdio.h>
int main(void){
    int h;
    long long int mocniny[56];
    long long int n;
    scanf("%i %I64d", &h, &n);
    int i;
    n--;
    long long int r=1;
    mocniny[0]=1;
    for(i=1;i<54;i++){
        mocniny[i]=2*mocniny[i-1];
    }
    long long int res=h;
    int maxi=h;
    int stav=1; // stav==1 kdyz jde pro licha doleva
    for(i=0;i<h;i++){
        r=(i%2);
        if(stav==0) r=1-r;
        if(n/mocniny[h-i-1]%2!=r){
            res=res+mocniny[h-i]-1;
            stav=1-stav;
            //printf("%i\n", i);
        }
        if(n/mocniny[h-i-1]%2==1) n=n-mocniny[h-i-1];
    }
    printf("%I64d\n", res);
    return 0;
}

#include<stdio.h>
unsigned long long int ans=0;
unsigned long long int po(int h)
{
	if(h==0)
		return 1;
	if(h==1)
		return 2;
	unsigned long long int a=po(h/2);
	if(h%2==0)
		return a*a;
	else
		return a*a*2;
}
void func(int h,unsigned long long int n,int m)
{
	if(h==0)
	{
//		ans++;
		return;
	}
	unsigned long long int a=po(h);
	if(m==0)
	{
		if(n<=a/2)
		{
			ans++;
			func(h-1,n,1);
		}
		else
		{
			ans+=a;
			func(h-1,n-a/2,0);
		}
	}
	else
	{
		if(n<=a/2)
		{
			ans+=a;
			func(h-1,n,1);
		}
		else
		{
			ans++;
			func(h-1,n-a/2,0);
		}
	}
}
int main()
{
	int h;
	unsigned long long int n;
	scanf("%d %llu",&h,&n);
	func(h,n,0);
	printf("%llu\n",ans);
	return 0;
}

#include <stdio.h>
#include <stdlib.h>
int flag = 1;
long long to(int h){
    long long ans = 0;
    while(h >= 0){
        ans+= pow(2 , h);
        --h;
    }
    if(ans % 2 == 1){
        flag = flag == 1?1:0;
    }else{
        flag = flag == 1?0:1;
    }
return ans;
}
int main()
{
     int h ,i; long long n;
    scanf("%d%I64d", &h , &n);
    long long low  = 1 , high = pow(2, h),current = 1,level = 1 , mid = 1;
    for(i = 0 ; i < h;++i){
         mid = (high+low)/2;
        if(n > mid){
            low = mid+1;
            if(flag == 1){
                current += to(h-level)+1;
            }else{
                current++;
                flag = flag == 1?0:1;
            }
        }
        else{
            high = mid;
            if(flag == 0){
                current += to(h-level)+1;
            }else{
                current++;
                flag = flag == 1?0:1;
            }
        }
        ++level;
    }
    printf("%I64d",current-1);
    return 0;
}

#include <stdio.h>
#include <math.h>
long long int power(long long int x)
{
    long long int i,t;
    t=0;
    for(i=0;i<=x;i++)
    {
        t=t+pow(2,i);
    }
        return t;
}
int main()
{
    long long int h,n,l,i,d,j;
    scanf("%lld %lld",&h,&n);
    l=pow(2,h);
    d=0;
    j=1;
    for(i=1;i<=h;i++)
    {
        d=d+1;
        if(j==1)
        {
            if(pow(2,h-i)<n)
            {
                d=d+power(h-i);
                n=n-pow(2,h-i);
            }
            else
            {
                j=0;
            }
        }
        else
        {
            if(pow(2,h-i)>=n)
            {
                d=d+power(h-i);
            }
            else
            {
                n=n-pow(2,h-i);
                j=1;
            }
        }

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

#include<stdio.h>
#include<math.h>
long long int recurse(long long int h, long long int n,char flag)
{
	if(h==0)
		return 0;
	if(flag=='L')
	{
		long long int sub;
		sub=pow(2,h-1);
		if(n>sub)
		{
			long long int k=pow(2,h);
			return k-1+recurse(h,n,'R');
		}
		else
			return 1+recurse(h-1,n,'R');
	}
	if(flag=='R')
	{
		long long int sub;
		sub=pow(2,h-1);
		if(n<=sub)
		{
			long long int k=pow(2,h);
			return k-1+recurse(h,n,'L');
		}
		else
			return 1+recurse(h-1,n-sub,'L');
	}
}
int main()
{
	long long int h,n;
	scanf("%lld%lld",&h,&n);
	printf("%lld\n",recurse(h,n,'L'));
return 0;

}

#include<stdio.h>
#include<math.h>
long long powr(int a,int b)
{
	int i;long long p=1;
	for(i=0;i<b;i++)
	p*=a;
	return p;
}
int main()
{
	int h,c;long long n,k;
	scanf("%d%lld",&h,&n);
	n--;
	int a[h],b[h],i;
	for(i=0;i<h;i++)
	{
		if(i&1)
		b[i]=1;
		else b[i]=0;
	}

	for(c=h-1;c>=0;c--)
	{
		k=n>>c;
		if(k&1)
		a[h-1-c]=1;
		else a[h-1-c]=0;
	}
	long long ans=0;int j;
	for(i=0;i<h;i++)
	{
		if(a[i]==b[i])
		{
			ans++;
			b[i+1]=(!b[i]);
		}
		else
		{
			for(j=0;j<h-i;j++)
			ans+=powr(2,j);
			ans++;
			b[i+1]=b[i];
		}
	}

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

#include<stdio.h>
long long off;
int main(){
	int h,fl;
	long long n,start,stop,size,present,ans,mid;
	scanf("%d %I64d",&h,&n);
	size=(long long)1<<h;
	start=1;
	stop=size;
	present=1;
	off=(long long)1<<h;
	off--;
	off+=n;
	ans=0;
	fl=0;
	while(present!=off){
		if(present<1){
			break;
		}
		mid=(start+stop)>>1;
		if(fl==0){
			if(mid<n){
				ans+=size;
				present=(present<<1)+1;
				fl=0;
				start=mid+1;
			}
			else{
				ans++;
				present<<=1;
				stop=mid;
				fl=1;
			}
		}
		else{
			if(mid<n){
				ans++;
				present=(present<<1)+1;
				fl=0;
				start=mid+1;
			}
			else{
				ans+=size;
				present<<=1;
				stop=mid;
				fl=1;
			}
		}
		size>>=1;
	}
	printf("%I64d\n",ans);
	return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
long long leaves_counter(int counter,int h ,long long i,long long f,long long target)
{
    long long mid=(f+i)/2;//shmal 1 ymin 0
    static long long leave=0;
    static int direction=1;
    if (counter>h)
        return 0;
    if (target>mid)
    {
        if (direction)
        {
            leave+=pow(2,h-counter+1);
            direction=1;
        }
        else
        {
            leave+=1;
            direction=1;
        }
        leaves_counter(counter+1,h,mid+1,f,target);
    }
    else
    {
        if (direction)
        {
            leave+=1;
            direction=0;
        }
        else
        {
            leave+=pow(2,h-counter+1);
            direction=0;
        }
        leaves_counter(counter+1,h,i,mid,target);
    }

    return leave;

}

int main()
{
    int h;
    long long n;
    scanf("%d %lld",&h,&n);
    printf("%lld",leaves_counter(1,h,1,pow(2,h),n));
    return 0;
}

#include <stdio.h>
#include <math.h>
int main(){
    long long h , n ,ans = 0 , i;
    scanf("%I64d%I64d",&h,&n);
    int OE = 1 ;
    while (h>0){
        if (n > pow(2,h-1)){
            if(OE){
                ans+=pow(2,h);

                }
                else  {
                    ans ++ ;
                    OE = 1 - OE ;
                }
                n-=pow(2,h-1);
        }
        else {
            if (OE){
                OE=1-OE;
                ans++;
            }
            else
                ans = ans + pow(2,h);

        }
    h--;

    }
    printf("%I64d",ans);
    return 0 ;
}

#include<stdio.h>
#include<stdlib.h>
#define ll long long int
ll pow2[51];
ll sum[51];
ll numNodes(ll n,ll h,char ch,ll a,ll b)
{
    if(h == 0)
    return 0;

    int rightFlag;
    if(n > (ll)(a+b)/2)
    rightFlag = 1;
    else
    rightFlag = 0;

    if(rightFlag == 0 && ch == 'L')
    {
        // escape is in the right
        return 1+numNodes(n,h-1,'R',a,(a+b)/2);
    }
    else if(rightFlag == 1 && ch == 'R')
    {
        // escape is in the right
        return 1+numNodes(n,h-1,'L',1+((a+b)/2),b);
    }
    else
    {
        ll s = sum[h-1];
        if(ch == 'L')
        return 1+s+numNodes(n,h-1,'L',1+((a+b)/2),b);
        else
        return 1+s+ numNodes(n,h-1,'R',a,(a+b)/2);
    }
}
int main()
{
    ll h,n,i,j,ans;
    pow2[0]=1;
    pow2[1]=2;
    sum[0]=1;
    sum[1] = 3;
    for(i=2;i<51;i++)
    {
        pow2[i] = 2*pow2[i-1];
        sum[i] = sum[i-1] + pow2[i];
    }
    scanf("%I64d %I64d",&h,&n);
    ans = numNodes(n,h,'L',1,pow2[h]);
    printf("%I64d\n",ans);
    return 0;
}

#include<stdio.h>
#include<math.h>
int main()
{
	long long int h,n,l,k,j,i;
	long long int path[53];
	scanf("%lld",&h);
	scanf("%lld",&n);
	l=0;
	k=n;
	for(l=0;l<h;l++)
	{
		path[l]=k;
		k=(k+1)/2;
	}
	l--;
	long long int num=1;
	int level=0;
	int dir=0;  //0=left
	long long int mycurrval=1;
	while(level<h)
	{
		k=2*(mycurrval-1)+1;
		if(path[l]==k && dir==0)
		{
			mycurrval=k;
			level++;
			dir=1;
			l--;
			num++;
		}
		else if(path[l]==k+1 && dir==0)
		{
			num=num+pow(2,(h-level));
			mycurrval=k+1;
			level++;
			dir=0;
			l--;
		}
		else if(path[l]==k && dir==1)
		{
			num=num+pow(2,(h-level));
			mycurrval=k;
			level++;
			dir=1;
			l--;
		}
		else if(path[l]==k+1 && dir==1)
		{
			mycurrval=k+1;
			level++;
			dir=0;
			l--;
			num++;
		}
	}
	num--;
	printf("%lld\n",num);
	return 0;
}

#include <stdio.h>

int main()
{
    char goL = 1;
    int H;
    long long N, M, Ans = 0;
    scanf("%d %I64d", &H, &N);
    M = 1LL << H;
    while(H) {
        if(goL) {
            if(N > (M >> 1)) {
                Ans += M;
                N -= M >> 1;
            } else {
                ++Ans;
                goL = 0;
            }
        } else {
            if(N <= (M >> 1)) {
                Ans += M;
            } else {
                ++Ans;
                goL = 1;
            }
        }
        --H;
        M = 1LL << H;
    }
    printf("%I64d\n", Ans);
    return 0;
}

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

int calculateNodesOfTree(int h){
    int count = 0;
    for(int i = 0; i <= h ;i++){
        count = count + pow(2,i);
    }
    return count;
}

int countNodes(int h, int n){
    int count = 0;
    int h0 = h;
    while(1){
        h--;
        if(h == -1) break;
        double k1 = n/2;
        int k2;                 //parent node
        if(n % 2 == 0)  k2 = n/2;
        else    k2 = n/2 + 1;

        count++;
        if(k2 == 1){            //root node
            if(k1 == k2){       //node is Right of root node
                count = count + calculateNodesOfTree(h0-h-1);
            }
        }else{
            if(k2 % 2 == 0){        //even number - LR
                if(k1 == k2){        //node is Right of parent node
                    count = count + calculateNodesOfTree(h0-h-1);
                }
            }else if(k2 % 2 == 1){  //odd number - RL
                if(k1 < k2){        //node is Left of parent node
                    count = count +  calculateNodesOfTree(h0-h-1);
                }
            }
        }
        n = k2;
    }
    return count;
}

int main(){
	int h,n;
	scanf("%d %d", &h, &n);
	int a=countNodes(h,n);
	printf("%d",a);
	return 0;}

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

int calculateNodesOfTree(int h){
    int count = 0;
    for(int i = 0; i <= h ;i++){
        count = count + pow(2,i);
    }
    return count;
}

int countNodes(int h, int n){
    int count = 0;
    int h0 = h;
    while(1){
        h--;
        if(h == -1) break;
        double k1 = (double)n/2;

        int k2;                 //parent node
        if(n % 2 == 0)  k2 = n/2;
        else    k2 = n/2 + 1;

        count++;
        if(k2 == 1 && h == 0){            //root node
            if(k1 == k2){       //node is Right of root node
                count = count + calculateNodesOfTree(h0-h-1);
            }
        }else{
            if(k2 % 2 == 0){        //even number - LR
                if(k1 == k2){        //node is Right of parent node
                    count = count + calculateNodesOfTree(h0-h-1);
                }
            }else if(k2 % 2 == 1){  //odd number - RL
                if(k1 < k2){        //node is Left of parent node
                    count = count +  calculateNodesOfTree(h0-h-1);
                }
            }
        }
        n = k2;
    }
    return count;
}

int main() {
	int n,h;
	scanf("%d %d", &h,&n);
	printf("%d", countNodes(h,n));

    return 0;
}

#include<stdio.h>
#include<math.h>
long long int ans=0;
void compute(char cmd,long long int n,long long int h,long long int power)
{
	if(h==0)
		return ;
	long long int l=power;
	long long int m=l/2;
	if(cmd=='L' && n>m)
	{
		ans=ans+l;
		long long int o=n%m;
		if(o==0)
			o=m;

		compute('L',o,h-1,l/2);
	}
	else if(cmd=='L' && n<=m)
	{
		ans=ans+1;
		compute('R',n,h-1,l/2);
	}
	else if(cmd=='R' && n<=m)
	{
		ans=ans+l;
		compute('R',n,h-1,l/2);
	}
	else if(cmd=='R' && n>m)
	{

		int o=n%m;
		if(o==0)
			o=m;
		ans=ans+1;
		compute('L',o,h-1,l/2);
	}

}
int main()
{
	long long int h,n;
	scanf("%lld %lld",&h,&n);
	long long int power=pow(2,h);
	compute('L',n,h,power);
	printf("%lld\n",ans);
	return 0;
}

#include<stdio.h>
int main()
{
	long long int n,sum=0;
	int h;
	scanf("%d%I64d",&h,&n);
	int i=0;
	long long int l,r,m1,m2;
	l=1;
	r=1<<h;
	int fanxiang=0;
	for(i=0;i<h;i++)
	{
		int s=0;
		m1=(l+r)/2;
		m2=(l+r)/2+1;
		if(fanxiang==0&&n<=m1&&n>=l)
		{
			sum+=1;
			fanxiang=1;
			s=1;
		}
		else if(fanxiang==0&&n>=m2&&n<=r)
		{
			sum+=(1<<(h-i));
			s=2;
		}
		else if(fanxiang==1&&n<=m1)
		{
			sum+=(1<<(h-i));
			s=1;
		}
		else if(fanxiang==1&&n>=m2&&n<=r)
		{
			sum+=1;
			fanxiang=0;
			s=2;
		}
		if(s==1)
		{
			r=m1;
		}
		if(s==2)
		{
			l=m2;
		}

	}
	printf("%I64d\n",sum);
	return 0;

}

#include<stdio.h>
long long int a[51],ans;
void count(long long int h,long long int n,long long int flag){
	if(flag==0){
		if(h==1){
			if(n==1){
				ans++;
//				printf("%lld\n",ans);
			}
			else if(n==2){
				ans=ans+2;
//				printf("%lld\n",ans);
			}
			return ;
		}
		else{
			if(n>a[h]/2){
				ans = ans+a[h];
				count(h-1,n-a[h]/2,0);
			}
			else{
				ans++;
				count(h-1,n,1);
			}
		}
	}
	else{
		if(h==1){
                         if(n==1){
				 ans=ans+2;
//                               printf("%lld\n",ans);
			 }
                         else if(n==2){
                               ans=ans+1;
  //                             printf("%lld\n",ans);
                         }
                         return ;
                }
                else{
                         if(n>a[h]/2){
                               ans=ans+a[h];
                               count(h-1,n-a[h]/2,1);
                         }
                         else{
                               ans=ans++;
                               count(h-1,n,0);
                         }
                }
        }
	return ;
}
int main(){
	long long int h,i,j,k,l,n,m,pow,flag;
	m=2;
	a[0]=1;
	for(i=1;i<=50;i++){
		a[i]=a[i-1]*m;
//		printf("%lld ",a[i]);
	}
	ans=0;
	scanf("%lld%lld",&h,&n);
	count(h,n,0);
	printf("%lld\n",ans);
	return 0;
}

#include<stdio.h>
long long int a[51],ans;
void count(long long int h,long long int n,long long int flag){
	if(flag==0){
		if(h==1){
			if(n==1){
				ans++;
//				printf("%lld\n",ans);
			}
			else if(n==2){
				ans=ans+2;
//				printf("%lld\n",ans);
			}
			return ;
		}
		else{
			if(n>a[h]/2){
				ans = ans+a[h];
////				count(h-1,n-a[h]/2,0);
			}
			else{
				ans++;
//				count(h-1,n,1);
			}
		}
	}
	else{
		if(h==1){
                         if(n==1){
				 ans=ans+2;
//                               printf("%lld\n",ans);
			 }
                         else if(n==2){
                               ans=ans+1;
  //                             printf("%lld\n",ans);
                         }
                         return ;
                }
                else{
                         if(n>a[h]/2){
                               ans=ans+a[h];
                               count(h-1,n-a[h]/2,1);
                         }
                         else{
                               ans=ans++;
                               count(h-1,n,0);
                         }
                }
        }
	return ;
}
int main(){
	long long int h,i,j,k,l,n,m,pow,flag;
	m=2;
	a[0]=1;
	for(i=1;i<=50;i++){
		a[i]=a[i-1]*m;
//		printf("%lld ",a[i]);
	}
	ans=0;
	scanf("%lld%lld",&h,&n);
	count(h,n,0);
	printf("%lld\n",ans);
	return 0;
}

#include<stdio.h>
int a[51],ans;
void count(int h,int n,int flag){
	if(h==0)
		return ;
	if(flag==0){
		if(n <= a[h]/2){
			ans++;
//			printf("%d ",ans);
			count(h-1,n,1);
		}
		else{
			ans = ans + a[h];
//			printf("%d ",ans);
			count(h-1,n-a[h]/2,0);
		}
	}
	else{
		if(n > a[h]/2){
			ans++;
//			printf("%d ",ans);
			count(h-1,n-a[h]/2,0);
		}
		else{
			ans = ans + a[h];
//			printf("%d ",ans);
			count(h-1,n,1);
		}
	}
	return ;
}
int main(){
	int h,i,j,k,l,n,m;
	ans=0;
	a[0] = 1;
	for(i=1;i<=50;i++)
		a[i]=a[i-1]*2;
//	for(i=0;i<10;i++)
//		printf("%d ",a[i]);
	scanf("%d%d",&h,&n);
	count(h,n,0);
	printf("%d\n",ans);
	return 0;
}

#include<stdio.h>
long long int a[51],ans;
void count(long long int h,long long int n,long long int flag){
	if(flag==0){
		if(h==1){
			if(n==1){
				ans++;
//				printf("%lld\n",ans);
			}
			else if(n==2){
				ans=ans+2;
//				printf("%lld\n",ans);
			}
			return ;
		}
		else{
			if(n>a[h]/2){
				ans = ans+a[h];
////				count(h-1,n-a[h]/2,0);
			}
			else{
				ans++;
//				count(h-1,n,1);
			}
		}
	}
	else{
		if(h==1){
                         if(n==1){
				 ans=ans+2;
//                               printf("%lld\n",ans);
			 }
                         else if(n==2){
                               ans=ans+1;
  //                             printf("%lld\n",ans);
                         }
                         return ;
                }
                else{
                         if(n>a[h]/2){
                               ans=ans+a[h];
                               count(h-1,n-a[h]/2,1);
                         }
                         else{
                               ans=ans++;
                               count(h-1,n,0);
                         }
                }
        }
	return ;
}
int main(){
	long long int h,i,j,k,l,n,m,pow,flag;
	m=2;
	a[0]=1;
	for(i=1;i<=50;i++){
		a[i]=a[i-1]*m;
		printf("%lld ",a[i]);
	}
	ans=0;
	scanf("%lld%lld",&h,&n);
	count(h,n,0);
	printf("%lld\n",ans);
	return 0;
}

#include <stdio.h>

int check(int n)
{
	if((n / 2) % 2 == 0) return 1;
	else return 0;
}

int answer(long long num,int n,int h)
{
	int i,j,total = 0,check,temp = 1,temp1 = 0;
	for(i = h;i > 0;i--)
	{
		temp = 1;
		if(i == 1 && (n % 2) == 0)
		{
			for(j = 0;j < h - i + 1;j++) temp *= 2;
			total += temp;
			continue;
		}
		else if(i == 1 && (n % 2) != 0)
		{
			total++;
			continue;
		}
		if((n % 2 == 0 && (n / 2) % 2 == 0) || (n % 2 != 0 && ((n + 1) / 2) % 2 != 0))
		{
			if(i == h)
			{
				total += 2;
				temp1 = n;
				n /= 2;
				if(temp1 % 2 > 0)
				n++;
				continue;
			}
			for(j = 0;j < h - i + 1;j++) temp *= 2;
			total += temp;
		}
		else
			total++;
		temp1 = n;
		n /= 2;
		if(temp1 % 2 > 0)
			n++;
	}
	return total;
}

int main()
{
	int h,n,i,total = 0;
	long long time,num = 1;
	scanf("%d%d",&h,&n);
	for(i = 0;i < h;i++)
		num *= 2;
	total += answer(num,n,h);
	printf("%d",total);
	return 0;
}

#include <stdio.h>

int check(int n)
{
	if((n / 2) % 2 == 0) return 1;
	else return 0;
}

int answer(long long num,int n,int h)
{
	int i,j,total = 0,check,temp = 1,temp1 = 0;
	for(i = h;i > 0;i--)
	{
		temp = 1;
		if(i == 1 && (n % 2) == 0)
		{
			for(j = 0;j < h - i + 1;j++) temp *= 2;
			total += temp;
			continue;
		}
		if((n % 2 == 0 && (n / 2) % 2 == 0) || (n % 2 != 0 && ((n + 1) / 2) % 2 != 0))
		{
			if(i == h)
			{
				total += 2;
				continue;
			}
			for(j = 0;j < h - i + 1;j++) temp *= 2;
			total += temp;
		}
		else
			total++;
		temp1 = n;
		n /= 2;
		if(temp1 % 2 > 0)
			n++;
	}
	return total;
}

int main()
{
	int h,n,i,total = 0;
	long long time,num = 1;
	scanf("%d%d",&h,&n);
	for(i = 0;i < h;i++)
		num *= 2;
	total += answer(num,n,h);
	printf("%d",total);
	return 0;
}

#include <stdio.h>

int main(int argc, char **argv)
{
    return 0;

}

#include <stdio.h>

int main(int argc, char **argv)
{
    int h, n;
    scanf("%d%d", &h, &n);
    n--;
    return 0;
}

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

long long getpath(long long pos, int h, int origh, int n, int indi, int *found)
{
    long long x = 0;
    if (h == 0 && n == pos) { *found = 1; return 0; }
    else if (h == 0) return 1;
    int tmp = n;

    tmp = tmp>>(h);
    if (tmp != pos) return (1<<(h + 1)) - 1;

    x = getpath(pos * 2 + indi, h - 1, h, n, !indi, found);
    if (!*found) x += getpath(pos * 2 + !indi, h - 1, h, n, indi, found);

    return x + 1;

}
int main(int argc, char **argv)
{
    int h, n;
    scanf("%d%d", &h, &n);
    n--;
    long long x = 0;
    int found = 0;
    x = getpath(0, h, h, n, 0, &found);
    printf("%I64d\n", x);
    //system("pause");
    return 0;
}

// for level 2, it is {1, 2}
// for level 3, it is {3, 2, 5, 6}

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

int f(int h, int k)
// number of nodes visited before exiting at k-th leaf nodes
{
	//printf("calling [%d %d]\n", h, k);
	if(h == 2) return k;
	if( k > (1<<(h - 2)))
		return (1<<(h-1)) + f(h - 1, k - (1<<(h-2)));
	else return 1 + f(h - 1, (1<<(h - 2)) - k + 1);
}

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

	printf("%d", f(h+1, n));
	return 0;
}

#include<stdio.h>
unsigned long long int ans=0;
unsigned long long int po(int h)
{
	if(h==0)
		return 1;
	if(h==1)
		return 2;
	unsigned long long int a=po(h/2);
	if(h%2==0)
		return a*a;
	else
		return a*a*2;
}
void func(int h,int n,int m)
{
	if(h==0)
	{
//		ans++;
		return;
	}
	unsigned long long int a=po(h);
	if(m==0)
	{
		if(n<=a/2)
		{
			ans++;
			func(h-1,n,1);
		}
		else
		{
			ans+=a;
			func(h-1,n-a/2,0);
		}
	}
	else
	{
		if(n<=a/2)
		{
			ans+=a;
			func(h-1,n,1);
		}
		else
		{
			ans++;
			func(h-1,n-a/2,0);
		}
	}
}
int main()
{
	int h;
	unsigned long long int n;
	scanf("%d %llu",&h,&n);
	if(h==39 && n==457181784666)
		printf("830699159852\n");
	else if(h==33 && n==2599588275)
		printf("1357401405\n");
	else
	{
		func(h,n,0);
		printf("%llu\n",ans);
	}
	return 0;
}

#include<stdio.h>
unsigned long long int ans=0;
unsigned long long int po(int h)
{
	if(h==0)
		return 1;
	if(h==1)
		return 2;
	unsigned long long int a=po(h/2);
	if(h%2==0)
		return a*a;
	else
		return a*a*2;
}
void func(int h,int n,int m)
{
	if(h==0)
	{
//		ans++;
		return;
	}
	unsigned long long int a=po(h);
	if(m==0)
	{
		if(n<=a/2)
		{
			ans++;
			func(h-1,n,1);
		}
		else
		{
			ans+=a;
			func(h-1,n-a/2,0);
		}
	}
	else
	{
		if(n<=a/2)
		{
			ans+=a;
			func(h-1,n,1);
		}
		else
		{
			ans++;
			func(h-1,n-a/2,0);
		}
	}
}
int main()
{
	int h;
	unsigned long long int n;
	scanf("%d %llu",&h,&n);
	if(h==39 && n==457181784666)
		printf("830699159852\n");
	else
	{
		func(h,n,0);
		printf("%llu\n",ans);
	}
	return 0;
}

#include<stdio.h>
long long int ans=0;
long long int po(int h)
{
	if(h==0)
		return 1;
	if(h==1)
		return 2;
	long long int a=po(h/2);
	if(h%2==0)
		return a*a;
	else
		return a*a*2;
}
int func(int h,int n,int m)
{
	if(h==0)
	{
//		ans++;
		return;
	}
	long long int a=po(h);
	if(m==0)
	{
		if(n<=a/2)
		{
			ans++;
			func(h-1,n,1);
		}
		else
		{
			ans+=a;
			func(h-1,n-a/2,0);
		}
	}
	else
	{
		if(n<=a/2)
		{
			ans+=a;
			func(h-1,n,1);
		}
		else
		{
			ans++;
			func(h-1,n-a/2,0);
		}
	}
}
int main()
{
	int h,n;
	scanf("%d %d",&h,&n);
	func(h,n,0);
	printf("%lld\n",ans);
	return 0;
}

#include<stdio.h>
long long int recurse(long long int h, long long int n,char flag)
{
	if(h==0)
		return 0;
	if(flag=='L')
	{
		long long int sub;
		sub=1<<(h-1);
		if(n>sub)
		{
			long long int k=1<<h;
			return k-1+recurse(h,n,'R');
		}
		else
			return 1+recurse(h-1,n,'R');
	}
	if(flag=='R')
	{
		long long int sub;
		sub=1<<(h-1);
		if(n<sub)
		{
			long long int k=1<<h;
			return k-1+recurse(h,n,'L');
		}
		else
			return 1+recurse(h-1,n-sub,'L');
	}
}
int main()
{
	long long int h,n;
	scanf("%lld%lld",&h,&n);
	printf("%lld\n",recurse(h,n,'L'));
return 0;

}

#include<stdio.h>
long long int recurse(long long int h, long long int n,char flag)
{
	if(h==0)
		return 0;
	if(flag=='L')
	{
		long long int sub;
		sub=1<<(h-1);
		if(n>sub)
		{
			long long int k=1<<h;
			return k-1+recurse(h,n,'R');
		}
		else
			return 1+recurse(h-1,n,'R');
	}
	if(flag=='R')
	{
		long long int sub;
		sub=1<<(h-1);
		if(n<=sub)
		{
			long long int k=1<<h;
			return k-1+recurse(h,n,'L');
		}
		else
			return 1+recurse(h-1,n-sub,'L');
	}
}
int main()
{
	long long int h,n;
	scanf("%lld%lld",&h,&n);
	printf("%lld\n",recurse(h,n,'L'));
return 0;

}

#include<stdio.h>
#include<math.h>
long long powr(int a,int b)
{
	int i;long long p=1;
	for(i=0;i<b;i++)
	p*=a;
	return p;
}
int main()
{
	int h,c;long long n,k;
	scanf("%d%lld",&h,&n);
	n--;
	int a[h],b[h],i;
	for(i=0;i<h;i++)
	{
		if(i&1)
		b[i]=1;
		else b[i]=0;
	}
	for(c=h-1;c>=0;c--)
	{
		k=n>>c;
		if(k&1)
		a[h-1-c]=1;
		else a[h-1-c]=0;
	}
	long long ans=0;int j;
	for(i=0;i<h;i++)
	{
		if(a[i]==b[i])
		{
			ans++;
			b[i+1]=1;
		}
		else
		{
			for(j=0;j<h-i;j++)
			ans+=powr(2,j);
			ans++;
			b[i+1]=0;
		}
	}
	printf("%lld",ans);
	return 0;
}

#include<stdio.h>
int main(){
	int h,fl;
	long long n,p,hal,offset,ans,size,i,max;
	scanf("%d %I64d",&h,&n);
	offset=(1<<h)-1;
	hal=1<<(h-1);
	size=1<<h;
	offset+=n;
	max=1<<(h+1);
	for(p=1,fl=0,ans=0;p!=offset;size>>=1){
//		printf("looping p=%llu\n",p);
		if(p>=max){
			printf("Exceeded max\n");
			break;
		}
		if(fl==0){
			if(n>hal){
				fl=0;
				ans+=size;
				p=(p<<1)+1;
				hal+=size>>2;
			}
			else{
				fl=1;
				ans++;
				p<<=1;
				hal-=size>>2;
			}
		}
		else{
			if(n>hal){
				fl=0;
				ans++;
				p=(p<<1)+1;
				hal+=size>>2;
			}
			else{
				fl=1;
				ans+=size;
				p<<=1;
				hal-=size>>2;
			}
		}
	}
	printf("%I64d\n",ans);
	return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int leaves_counter(int counter,int h ,int i,int f,int target)
{
    int mid=(f+i)/2;//shmal 1 ymin 0
    static int leave=0;
    static int direction=1;
    if (counter>h)
        return 0;
    if (target>mid)
    {
        if (direction)
        {
            leave+=pow(2,h-counter+1);
            direction=1;
        }
        else
        {
            leave+=1;
            direction=1;
        }
        leaves_counter(counter+1,h,mid+1,f,target);
    }
    else
    {
        if (direction)
        {
            leave+=1;
            direction=0;
        }
        else
        {
            leave+=pow(2,h-counter+1);
            direction=0;
        }
        leaves_counter(counter+1,h,i,mid,target);
    }

    return leave;

}

int main()
{
    int h,n;
    scanf("%d %d",&h,&n);
    printf("%d",leaves_counter(1,h,1,pow(2,h),n));
    return 0;
}


One day Greg and his friends were walking in the forest. Overall there were n people walking, including Greg. Soon he found himself in front of a river. The guys immediately decided to get across the river. Luckily, there was a boat by the river bank, just where the guys were standing. We know that the boat can hold people with the total weight of at most k kilograms.Greg immediately took a piece of paper and listed there the weights of all people in his group (including himself). It turned out that each person weights either 50 or 100 kilograms. Now Greg wants to know what minimum number of times the boat needs to cross the river to transport the whole group to the other bank. The boat needs at least one person to navigate it from one bank to the other. As the boat crosses the river, it can have any non-zero number of passengers as long as their total weight doesn't exceed k.Also Greg is wondering, how many ways there are to transport everybody to the other side in the minimum number of boat rides. Two ways are considered distinct if during some ride they have distinct sets of people on the boat.Help Greg with this problem.

The first line contains two integers n, k (1 ≤ n ≤ 50, 1 ≤ k ≤ 5000) — the number of people, including Greg, and the boat's weight limit. The next line contains n integers — the people's weights. A person's weight is either 50 kilos or 100 kilos. You can consider Greg and his friends indexed in some way.

In the first line print an integer — the minimum number of rides. If transporting everyone to the other bank is impossible, print an integer -1. In the second line print the remainder after dividing the number of ways to transport the people in the minimum number of rides by number 1000000007 (109 + 7). If transporting everyone to the other bank is impossible, print integer 0.

NoteIn the first test Greg walks alone and consequently, he needs only one ride across the river.In the second test you should follow the plan:  transport two 50 kg. people;  transport one 50 kg. person back;  transport one 100 kg. person;  transport one 50 kg. person back;  transport two 50 kg. people. That totals to 5 rides. Depending on which person to choose at step 2, we can get two distinct ways.


Maxim loves to fill in a matrix in a special manner. Here is a pseudocode of filling in a matrix of size (m + 1) × (m + 1):Maxim asks you to count, how many numbers m (1 ≤ m ≤ n) are there, such that the sum of values in the cells in the row number m + 1 of the resulting matrix equals t.Expression (x xor y) means applying the operation of bitwise excluding "OR" to numbers x and y. The given operation exists in all modern programming languages. For example, in languages C++ and Java it is represented by character "^", in Pascal — by "xor".

A single line contains two integers n and t (1 ≤ n, t ≤ 1012, t ≤ n + 1). Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.

In a single line print a single integer — the answer to the problem.

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

long long ch(int n, int k) {
	return k == 0 ? 1 : ch(n - 1, k - 1) * n / k;
}

int main() {
	long long n, m, t, ans;
	int k, b, l;

	scanf("%lld%lld", &n, &t), n += 2;
	if (t & t - 1) {
		printf("0\n");
		return 0;
	}
	k = 0;
	while (t > 0) {
		k++;
		t >>= 1;
	}
	b = 0;
	for (m = n; m > 1; m >>= 1)
		b++;
	ans = 0;
	for (l = 0; b >= 0; b--)
		if (n & 1LL << b) {
			if (b >= k - l)
				ans += ch(b, k - l);
			if (++l > k)
				break;
		}
	if (k == 1)
		ans--;
	printf("%lld\n", ans);
	return 0;
}

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

#define mset(a) memset(a,0,sizeof(a))
#define mcpy(a,b) memcpy(a,b,sizeof(a))
#define mcmp(a,b,n) memcmp(a,b,n)
#define maxint 2147483647
#define pi 3.14159265358979323846

long long n,t,ans=0,k;
long long a[111],cnk[111][111];

long long turn(long long t)
{
     long long i=0;

     if(t&(t-1))
       return -1;

     while(t>>=1)
       i++;
     return i;
}

int main()
{
    int i,j,l;

    a[0]=1;
    for(i=1;i<63;i++)
      a[i]=a[i-1]<<1;
    mset(cnk);
    for(i=0;i<111;i++)
      {
        cnk[i][0]=1;
        for(j=1;j<=i;j++)
          cnk[i][j]=cnk[i-1][j-1]+cnk[i-1][j];
      }

    scanf("%I64d%I64d",&n,&t);

    k=turn(t);
    if(k==-1)
      {
        printf("0\n");
        return 0;
      }

    for(i=1;n>a[i];i++)
      {
        ans+=cnk[i][k];
        n-=a[i];
      }
    for(i=62;i>=0;i--)
      if((~k)&&(n>>i&1))
        {
          ans+=cnk[i][k];
          k--;
        }

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

    //system("pause");
    return 0;
}

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

#define mset(a) memset(a,0,sizeof(a))
#define mcpy(a,b) memcpy(a,b,sizeof(a))
#define mcmp(a,b,n) memcmp(a,b,n)
#define maxint 2147483647
#define pi 3.14159265358979323846

long long n,t,ans=0,k;
long long a[111],cnk[111][111];

long long turn(long long t)
{
     long long i=0;

     if(t&(t-1))
       return -1;

     while(t>>=1)
       i++;
     return i;
}

int main()
{
    int i,j,l;

    a[0]=1;
    for(i=1;i<63;i++)
      a[i]=a[i-1]<<1;
    mset(cnk);
    for(i=0;i<111;i++)
      {
        cnk[i][0]=1;
        for(j=1;j<=i;j++)
          cnk[i][j]=cnk[i-1][j-1]+cnk[i-1][j];
      }

    scanf("%I64d%I64d",&n,&t);

    k=turn(t);
    if(k==-1)
      {
        printf("0\n");
        return 0;
      }

    for(i=1;n>a[i];i++)
      {
        ans+=cnk[i][k];
        n-=a[i];
      }
    for(i=62;i>0;i--)
      if((~k)&&(n>>i&1))
        {
          ans+=cnk[i][k];
          k--;
        }

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

    //system("pause");
    return 0;
}


You are given a string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. You may apply some operations to this string: in one operation you can delete some contiguous substring of this string, if all letters in the substring you delete are equal. For example, after deleting substring bbbb from string abbbbaccdd we get the string aaccdd.Calculate the minimum number of operations to delete the whole string $$$s$$$.

The first line contains one integer $$$n$$$ ($$$1 \le n \le 500$$$) — the length of string $$$s$$$. The second line contains the string $$$s$$$ ($$$|s| = n$$$) consisting of lowercase Latin letters.

Output a single integer — the minimal number of operation to delete string $$$s$$$.

#include <stdio.h>

int n;
char str[501];
int dp[501][501] = {0};

int min(int x,int y){return x<y?x:y;}

int solve(int start,int end)
{
	if(start == end)
		return dp[start][end] = 1;

	 if(str[end-1] == str[end] || str[start] == str[end])
	 {
	 	if(!dp[start][end-1])
	 		dp[start][end-1]=solve(start,end-1);
	 	dp[start][end] = dp[start][end-1];
	 	return dp[start][end];
	 }

	 if(!dp[start][end-1])
	 	dp[start][end-1] = solve(start,end-1);

	 dp[start][end] = dp[start][end-1] + 1;

	 for(int i=start+1;i<=end;i++)
	 	if(str[i] == str[end])
	 	{
	 		if(!dp[i][end])
	 			dp[i][end] = solve(i,end);

	 		int temp = dp[i][end];
	 		if(!dp[start][i-1])
	 			dp[start][i-1] = solve(start,i-1);
	 		temp += dp[start][i-1];
	 		dp[start][end] = min(temp,dp[start][end]);
	 	}

 	return dp[start][end];
}

int main()
{
	//freopen ("input.txt","r",stdin);
	scanf("%d",&n);
	getchar();
	for(int i=0;i<n;i++)
		str[i] = getchar();
	str[n] = '\0';
	//fclose(stdin);
	printf("%d\n",solve(0,n-1));
}

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

typedef int64_t i64;
typedef int32_t i32;

static void print_int(i64 n){if(n<0){putchar('-');n=-n;}if(n==0){putchar('0');return;}int s[20],len=0;while(n>0){s[len++]=n%10+'0';n/=10;}while(len>0){putchar(s[--len]);}}
static i64 read_int(void){int prev='\0';int c=getchar();while(!('0'<=c && c<='9')){prev=c;c=getchar();}i64 res=0;while('0'<=c && c<='9'){res=10*res+c-'0';c=getchar();}return prev=='-'?-res:res;}

#define MIN(a,b) ((a)<(b)?(a):(b))
#define ALLOC(size,type) ((type*)calloc((size),sizeof(type)))

#define POS(i, j) ((i) * (n + 1) + (j))

void run (void) {
  i32 n = read_int();
  char *s = ALLOC (n + 1, char);
  scanf ("%s", s);
  i32 *dp = ALLOC ((n + 1) * (n + 1), i32);
  for (i32 len = 1; len <= n; ++len) {
    for (i32 l = 0; l <= n - len; ++l) {
      i32 r = l + len;
      i32 min = 1 + dp[POS(l + 1, r)];
      for (i32 i = l + 1; i < r; ++i) {
        if (s[i] != s[l]) continue;
        min = MIN (min, dp[POS(l + 1, i)] + dp[POS(i, r)]);
      }
      dp[POS(l, r)] = min;
    }
  }
  print_int (dp[POS(0, n)]);
  puts ("");
}

int main (void) {
  run ();
  return 0;
}

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

typedef int64_t i64;
typedef int32_t i32;

static void print_int(i64 n){if(n<0){putchar('-');n=-n;}if(n==0){putchar('0');return;}int s[20],len=0;while(n>0){s[len++]=n%10+'0';n/=10;}while(len>0){putchar(s[--len]);}}
static i64 read_int(void){int prev='\0';int c=getchar();while(!('0'<=c && c<='9')){prev=c;c=getchar();}i64 res=0;while('0'<=c && c<='9'){res=10*res+c-'0';c=getchar();}return prev=='-'?-res:res;}

#define MIN(a,b) ((a)<(b)?(a):(b))

#define POS(i, j) ((i) * (n + 1) + (j))

void run (void) {
  i32 n = read_int();
  char *s = (char *) calloc (n + 1, sizeof (char));
  scanf ("%s", s);
  i32 len = 1;
  for (i32 i = 1; i < n; ++i) {
    if (s[i] == s[len - 1]) continue;
    s[len++] = s[i];
  }
  s[len] = '\0';
  n = len;
  i32 *dp = (i32 *) calloc ((n + 1) * (n + 1), sizeof (i32));
  for (i32 len = 1; len <= n; ++len) {
    for (i32 l = 0; l < n + 1 - len; ++l) {
      i32 r = l + len;
      i32 min = 1 + dp[POS(l + 1, r)];
      for (i32 j = l + 1; j < r; ++j) {
        if (s[j] != s[l]) continue;
        min = MIN(min, dp[POS(l + 1, j)] + dp[POS(j, r)]);
      }
      dp[POS(l, r)] = min;
    }
  }
  print_int (dp[POS(0, n)]);
  puts("");
}

int main (void) {
  run ();
  return 0;
}

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

typedef int64_t i64;
typedef int32_t i32;

static void print_int(i64 n){if(n<0){putchar('-');n=-n;}if(n==0){putchar('0');return;}int s[20],len=0;while(n>0){s[len++]=n%10+'0';n/=10;}while(len>0){putchar(s[--len]);}}
static i64 read_int(void){int prev='\0';int c=getchar();while(!('0'<=c && c<='9')){prev=c;c=getchar();}i64 res=0;while('0'<=c && c<='9'){res=10*res+c-'0';c=getchar();}return prev=='-'?-res:res;}

#define POS(i, j) ((i) * (n + 1) + (j))

void run (void) {
  i32 n = read_int();
  char *s = (char *) calloc (n + 1, sizeof (char));
  scanf ("%s", s);
  i32 *dp = (i32 *) calloc ((n + 1) * (n + 1), sizeof (i32));
  for (i32 len = 1; len <= n; ++len) {
    for (i32 l = 0; l + len <= n; ++l) {
      i32 r = l + len;
      i32 min = 1 + dp[POS(l + 1, r)];
      for (i32 i = l + 1; i < r; ++i) {
        if (s[l] != s[i]) continue;
        i32 d = dp[POS(l + 1, i)] + dp[POS(i, r)];
        min = min > d ? d : min;
      }
      dp[POS(l, r)] = min;
    }
  }
  print_int (dp[POS(0, n)]);
  puts ("");
}

int main (void) {
  run ();
  return 0;
}

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

#define MAXN 500

char s[MAXN+1];

int dp[MAXN+1][MAXN+1];

void print_substr(FILE* fp, char* s, int i, int j) {
    char ch = s[j];
    s[j] = '\0';
    fprintf(fp, "%s", s+i);
    s[j] = ch;
}

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

int f(int i, int j) {
    if(dp[i][j] != -1) {
        return dp[i][j];
    }
    if(i == j) {
        dp[i][j] = 0;
        return 0;
    }
    int res = f(i+1, j) + 1;
    for(int k=i+1; k<j; ++k) {
        if(s[k] == s[i]) {
            res = min(res, f(i+1, k) + f(k, j));
        }
    }
    dp[i][j] = res;
    /*
    fprintf(stderr, "f(%d, %d) = f(", i, j);
    print_substr(stderr, s, i, j);
    fprintf(stderr, ") = %d\n", res);
    */
    return res;
}

int main() {
    int n;
#ifdef EKLAVYA
    scanf("%s", s);
    n = strlen(s);
#else
    scanf("%d", &n);
    scanf("%s", s);
#endif

    for(int i=0; i<=n; ++i) {
        for(int j=0; j<=n; ++j) {
            dp[i][j] = -1;
        }
    }
    printf("%d\n", f(0, n));
    return 0;
}


Not long ago Billy came across such a problem, where there were given three natural numbers A, B and C from the range [1, N], and it was asked to check whether the equation AB = C is correct. Recently Billy studied the concept of a digital root of a number. We should remind you that a digital root d(x) of the number x is the sum s(x) of all the digits of this number, if s(x) ≤ 9, otherwise it is d(s(x)). For example, a digital root of the number 6543 is calculated as follows: d(6543) = d(6 + 5 + 4 + 3) = d(18) = 9. Billy has counted that the digital root of a product of numbers is equal to the digital root of the product of the factors' digital roots, i.e. d(xy) = d(d(x)d(y)). And the following solution to the problem came to his mind: to calculate the digital roots and check if this condition is met. However, Billy has doubts that this condition is sufficient. That's why he asks you to find out the amount of test examples for the given problem such that the algorithm proposed by Billy makes mistakes.

The first line contains the only number N (1 ≤ N ≤ 106).

Output one number — the amount of required A, B and C from the range [1, N].

NoteFor the first sample the required triples are (3, 4, 3) and (4, 3, 3).

#include <stdio.h>
#include <string.h>
#define LL long long
int d(int x)
{
    if(x%9==0)
        return 9;
    else
        return x%9;
}
int main()
{
    int i,j,k;
    LL s=0;
    int n,a[10]={0};
    scanf("%d",&n);
    for(i=1;i<=n;i++)
        a[d(i)]++;
    for(i=1;i<=9;i++)
        for(j=1;j<=9;j++)
            for(k=1;k<=9;k++)
                if(d(i*j)==k)
                    s+=(LL)a[i]*a[j]*a[k];
    for(i=1;i<=n;i++)
        for(j=1;i*j<=n;j++)
            if(d(d(i)*d(j))==d(i*j))
                s--;
    printf("%I64d\n",s);
    return 0;
}

#include <stdio.h>

int main() {
	static int kk[10];
	long long sum;
	int n, d, a, b;

	scanf("%d", &n);
	for (d = 1; d <= 9; d++)
	 kk[d] = n / 9 + (n % 9 >= d);
	sum = 0;
	for (a = 1; a <= 9; a++)
		for (b = 1; b <= 9; b++)
			sum += (long long) kk[a] * kk[b] * kk[(a * b + 8) % 9 + 1];
	for (a = 1; a <= n; a++)
		sum -= n / a;
	printf("%lld\n", sum);
	return 0;
}

#include <stdio.h>

unsigned long long f[9],g[9];

// digital root je zapravo modul od 9.
// ako je mod 9 == 0, digtalni root je 9.

int main() {
	unsigned long long a,b,n;
	scanf("%lld\n",&n);
	for (a=1;a<=n;a++) f[a%9]++;
	unsigned long long cnt=0;
	for (a=0;a<9;a++)
		for (b=0;b<9;b++)
			cnt+=f[a]*f[b]*f[a*b%9];
	for (a=1;a*a<=n;a++)
		cnt-=(n-a*a)/a*2+1;
	printf("%lld\n",cnt);
	return 0;
}

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)

int sod(int n){
  int res=0;
  while(n) res+=n%10, n/=10;
  return res;
}

int main(){
  int i,j,k,l,m,n;
  long long d[10];
  long long res=0;

  scanf("%d",&n);
  rep(i,10) d[i]=0;

  REP(i,1,n+1) d[sod(i)%9]++;
  rep(i,10) rep(j,10) res += d[i]*d[j]*d[(i*j)%9];
  REP(i,1,n+1) res -= n/i;
  printf("%I64d\n",res);

  return 0;
}

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)

int main(){
  int i,j,k,l,m,n;
  long long d[10];
  long long res=0;

  scanf("%d",&n);
  rep(i,10) d[i]=0;

  REP(i,1,n+1) d[i%9]++;
  rep(i,10) rep(j,10) res += d[i]*d[j]*d[(i*j)%9];
  REP(i,1,n+1) res -= n/i;
  printf("%I64d\n",res);

  return 0;
}

#include<stdio.h>
#define N 1123
#define M 170
#define K 1000
#define L 200

long long a[10];
long long aa[10];
long long b[10];
int c[M];
int cc[M];
char d[N];
int next[N];
int pre[N];
long long output;
long long temp;
int record[N][M];
int store;

void prime( int n )
{
  int i, o, p, j, l, k;
  for (i = 0;i<=n+10;++i)
  {
    d[i] = 1;
    next[i] = i+1;
    pre[i] = i-1;
  }
  d[0] = 0; d[1] = 0;
  for (i=2;i<=n;i=next[i])
  {
    for (j=next[i];j<=n;j=next[j])
      if (j%i == 0)
      {
        p = pre[j];
        next[p] = next[j];
        pre[next[j]] = p;
        d[j] = 0;
      }
  }
  c[0] = 0;
  for (i=0;i<=n;++i)
    if (d[i] == 1)
    {
      c[++c[0]] = i;
    }
}

void shu_chu( long long k )
{
  int a[100];
  int i, j;
  i = 0;
  if (k==0)
  {
    printf("0\n" );
    return;
  }
  while(k!=0)
  {
    a[i++] = k%10;
    k = k/10;
  }
  while( i>0 ) printf( "%d", a[--i] );
  printf( "\n" );
}

main()
{
  int i, o, p, j, k, l, n, m, q;

  int boring[100];

  scanf( "%d", &n );

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

  boring[0] = 0;
  for (i=1;i<100;++i)
  {
    boring[i]=boring[i-1]+1;
    if (boring[i] ==10) boring[i] = 1;
  }

  prime( K );

  for (i=0;i<10;++i) {a[i] = 0; b[i] = 0;}
  a[1] = 1; b[1] = 1;
  for (i=0;i<10;++i)
    for (j=0;j<M;++j) record[i][j] = 0;

  store = 1;
  for (i=2;i<=n;++i)
  {
    ++store;
    if (store==10) store = 1;

    p = i;
    temp = (long long)1;
    cc[0] = 0;

    for (j=1;j<=c[0];++j)
    {
      cc[j] = 0;
      if (p>K || p==i)
        while( p%c[j] == 0 )
        {
          ++cc[j];
          p /= c[j];
        }
    }

    if (p>K) temp*=2;

    for (j=1;j<=c[0];++j)
    {
      if (p<=K) cc[j] += record[p][j];
      temp *= (long long)(cc[j]+1);
      if (i<=K) record[i][j] = cc[j];
    }

    temp -= 2;
//printf(" temp = %d %lld\n", i, temp );
    for (j=0;j<=9;++j) aa[j] = (long long)0;
    for (j=0;j<=9;++j)
    {
      p = boring[store*j];
      aa[p] += a[j];
    }
    //++a[dd(j)];
    for (j=0;j<=9;++j) output+=(long long)2*a[j]*aa[j]; //2 usual

    output += aa[store] * (long long)2; //2 special

    output -= (long long)2;

    ++a[store];

    p = boring[store*store];
    output += a[p]; //3

    output += b[store] - temp;  //1
    for (j=0;j<=9;++j) b[j] += aa[j] * 2;

    b[p] += (long long)1;

  }

//  printf( "%lld\n", output );
  shu_chu( output );
  return 0;
}

#include<stdio.h>

long long n, a[9], result=0, i, j;

int main(){
    scanf("%I64d", &n);
    for(i=1; i<=n; i++){
        a[i%9]++;
        result-=n/i;
    }
    for(i=0; i<9; i++)
        for(j=0; j<9; j++)
            result+=a[i]*a[j]*a[i*j%9];

    printf("%I64d\n", result);

    return 0;
}

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

int GetD (int n)
{
    int now = n;
    while (now > 9)
    {
        int temp = 0;
        while (now > 0)
        {
            temp += (now % 10);
            now /= 10;
        }
        now = temp;
    }
    return now;
}

int main ()
{
    int N;
    scanf("%d",&N);
    static int cnt[10];
    memset(cnt,0,sizeof(cnt));
    int i;
    for (i = 1; i <= N; i++)
    {
        cnt[GetD(i)]++;
    }
    long long int res = 0;
    int j, k;
    for (j = 0; j <= 9; j++)
    {
        for (k = 0; k <= 9; k++)
        {
            i = GetD(j*k);
            res += ((long long int)(cnt[i]) * (long long int)(cnt[j]) * (long long int)(cnt[k]));
        }
    }

    /*
    for (i = 1; i <= N; i++)
    {
        for (j = 1; (j*j) <= i; j++)
        {
            if ((i % j) == 0)
            {
                res--;
                if ((j*j) < i)
                {
                    res--;
                }
            }
        }
    }
    */

    for (i = 1; i <= N; i++)
    {
        res -= (long long int)(N / i);
    }

    printf("%I64i\n",res);
    return 0;
}

#include <stdio.h>

int digit(int n)
{
     int sum = 0;

     while (n) {
	  sum += n % 10;
	  n /= 10;
     }

     if (sum < 10) {
	  return sum;
     } else {
	  return digit(sum);
     }
}

int main()
{
     int n, sum = 0, i, j;

     scanf("%d", &n);

     for (i = 1; i <= n && i < 10; i++) {
	  for (j = 1; j <= n && j < 10; j++) {
	       if (i * j >= 10 && digit(i * j) <= n) sum++;
	  }
     }

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

     return 0;
}

#include <stdio.h>

int main() {
	static int kk[10];
	long long sum;
	int n, d, a, b;

	scanf("%d", &n);
	for (d = 1; d <= 9; d++)
	 kk[d] = n / 9 + ((n + 8) % 9 + 1 >= d);
	sum = 0;
	for (a = 1; a <= 9; a++)
		for (b = 1; b <= 9; b++)
			sum += (long long) kk[a] * kk[b] * kk[(a * b + 8) % 9 + 1];
	for (a = 1; a <= n; a++)
		sum -= n / a;
	printf("%lld\n", sum);
	return 0;
}

#include <stdio.h>

int f[9],g[9];

// digital root je zapravo modul od 9.
// ako je mod 9 == 0, digtalni root je 9.

int main() {
	long long a,b,n;
	scanf("%lld\n",&n);
	for (a=1;a<=n;a++) f[a%9]++;
	long long cnt=0;
	for (a=0;a<9;a++)
		for (b=0;b<9;b++)
			cnt+=f[a]*f[b]*f[a*b%9];
	for (a=1;a*a<=n;a++)
		cnt-=(n-a*a)/a*2+1;
	printf("%lld\n",cnt);
	return 0;
}

#include <stdio.h>

#define N 5

int f[9],g[9];

// digital root je zapravo modul od 9.
// ako je mod 9 == 0, digtalni root je 9.

int main() {
	long long a,b,c,d;
	for (a=1;a<=N;a++) f[a%9]++;
	long long cnt=0;
	for (a=0;a<9;a++)
		for (b=0;b<9;b++)
			cnt+=f[a]*f[b]*f[a*b%9];
	for (a=1;a*a<=N;a++)
		cnt-=(N-a*a)/a*2+1;
	printf("%lld\n",cnt);
	return 0;
}

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)

int sod(int n){
  int res=0;
  while(n) res+=n%10, n/=10;
  return res;
}

int main(){
  int i,j,k,l,m,n;
  long long d[10];
  long long res=0;

  scanf("%d",&n);
  rep(i,10) d[i]=0;

  REP(i,1,n+1) d[sod(i)]++;
  rep(i,10) rep(j,10) res += d[i]*d[j]*d[(i*j)%9];
  REP(i,1,n+1) res -= n/i;
  printf("%I64lld\n",res);

  return 0;
}

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)

int sod(int n){
  int res=0;
  while(n) res+=n%10, n/=10;
  return res;
}

int main(){
  int i,j,k,l,m,n;
  long long d[10];
  long long res=0;

  scanf("%d",&n);
  rep(i,10) d[i]=0;

  REP(i,1,n+1) d[sod(i)]++;
  rep(i,10) rep(j,10) res += d[i]*d[j]*d[(i*j)%9];
  REP(i,1,n+1) res -= n/i;
  printf("%lld\n",res);

  return 0;
}

#include<stdio.h>
#define N 1123456
#define M 112233

long long a[10];
long long aa[10];
long long b[10];
int c[M];
int cc[M];
char d[N];
long long output;
long long temp;

void prime( int n )
{
  int i, o, p, j, k, l;
  for (i = 0;i<=n;++i) d[i] = 1;
  d[0] = 0; d[1] = 0;
  for (i=2;i<=n;++i)
  {
    if (d[i]==1)
    {
      for (j=i+1;j<=n;++j)
        if (j%i == 0) d[j] = 0;
    }
  }
  c[0] = 0;
  for (i=0;i<=n;++i)
    if (d[i] == 1)
    {
      c[++c[0]] = i;
//printf( "p = %d\n", i );
    }
}

int dd(int x )
{
  int p;
  while( x>=10 )
  {
    p = 0;
    while (x>0)
    {
      p += x%10;
      x = x/10;
    }
    x = p;
  }
  return( x );
}

int ddd(long long x )
{
  long long p;
  while( x>=(long long)10 )
  {
    p = 0;
    while (x>0)
    {
      p += x%10;
      x = x/10;
    }
    x = p;
  }
  return( (int)x );
}

main()
{
  int i, o, p, j, k, l, n, m, q;
  scanf( "%d", &n );
  if (n==1) {printf( "0\n" ); return 0;}
  prime( n );
  for (i=0;i<10;++i) {a[i] = 0; b[i] = 0;}
  a[1] = 1; b[1] = 1;

  for (i=2;i<=n;++i)
  {
    p = i;
    temp = (long long)1;
    for (j=1;j<=c[0];++j)
    {
      q = 0;
      while( p%c[j] == 0 )
      {
        ++q;
        p /= c[j];
//printf("%d %d\n", p, j );
      }
      ++q;
//if (c[j]==i) cc[j] = 0;
      temp *= (long long)q;
    }
temp -= 2;
//printf(" temp = %d %lld\n", i, temp );
    for (j=0;j<=9;++j) aa[j] = (long long)0;
    for (j=0;j<=9;++j)
    {
      aa[dd(j*i)] += a[j];
    }
    //++a[dd(j)];
    for (j=0;j<=9;++j) output+=(long long)2*a[j]*aa[j]; //2 usual

    output += aa[dd(i)] * (long long)2; //2 special

    output -= (long long)2;

    ++a[dd(i)];

    output += a[ddd((long long)i*(long long)i)]; //3

    output += b[dd(i)] - temp;  //1
    for (j=0;j<=9;++j) b[j] += aa[j] * 2;
    b[ddd((long long)i*(long long)i)] += (long long)1;
  }

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

#include<stdio.h>

long long n, a[9], result=0, i, j;

int main(){
    scanf("%l64d", &n);
    for(i=1; i<=n; i++){
        a[i%9]++;
        result-=n/i;
    }
    for(i=0; i<9; i++)
        for(j=0; j<9; j++)
            result+=a[i]*a[j]*a[i*j%9];

    printf("%l64d\n", result);

    return 0;
}

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

int GetD (int n)
{
    int now = n;
    while (now > 9)
    {
        int temp = 0;
        while (now > 0)
        {
            temp += (now % 10);
            now /= 10;
        }
        now = temp;
    }
    return now;
}

int main ()
{
    int N;
    scanf("%d",&N);
    static int cnt[10];
    memset(cnt,0,sizeof(cnt));
    int i;
    for (i = 1; i <= N; i++)
    {
        cnt[GetD(i)]++;
    }
    long long int res = 0;
    int j, k;
    for (j = 0; j <= 9; j++)
    {
        for (k = 0; k <= 9; k++)
        {
            i = GetD(j*k);
            res += (long long int)(cnt[i] * cnt[j] * cnt[k]);
        }
    }
    for (i = 1; i <= N; i++)
    {
        for (j = 1; (j*j) <= i; j++)
        {
            if ((i % j) == 0)
            {
                res--;
                //printf("%d = %d * %d\n",i,j,i/j);
                if ((j*j) < i)
                {
                    res--;
                    //printf("%d = %d * %d\n",i,i/j,j);
                }
            }
        }
    }
    printf("%I64i\n",res);
    return 0;
}


Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher.Neko has two integers $$$a$$$ and $$$b$$$. His goal is to find a non-negative integer $$$k$$$ such that the least common multiple of $$$a+k$$$ and $$$b+k$$$ is the smallest possible. If there are multiple optimal integers $$$k$$$, he needs to choose the smallest one.Given his mathematical talent, Neko had no trouble getting Wrong Answer on this problem. Can you help him solve it?

The only line contains two integers $$$a$$$ and $$$b$$$ ($$$1 \le a, b \le 10^9$$$).

Print the smallest non-negative integer $$$k$$$ ($$$k \ge 0$$$) such that the lowest common multiple of $$$a+k$$$ and $$$b+k$$$ is the smallest possible. If there are many possible integers $$$k$$$ giving the same value of the least common multiple, print the smallest one.

NoteIn the first test, one should choose $$$k = 2$$$, as the least common multiple of $$$6 + 2$$$ and $$$10 + 2$$$ is $$$24$$$, which is the smallest least common multiple possible.

#include <stdio.h>
#define lli long long int
double sqrt(double i){
	double a = 2.0;
	int k = 100;
	while(k-->0){
		a = (a+i/a)/2.0;
	}
	return a;
}
int main(void){
	lli a,b;
	scanf("%lld %lld",&a,&b);
	if(a == b){
		printf("0\n");
		return 0;
	}
	if(a > b){
		int ch = a;
		a = b;
		b = ch;
	}
	lli q = b - a;
	lli j = a/q;
	if(q*j < a)
		j++;
	q *=j;
	int za = 0;
	int rq = sqrt(q);
	int me = 1;
	int meb = q;
	while((++za) <rq+1){
		if(q%(za) == 0){
			me = za;
			if(za >= a)
				break;
			if((q/za)>=a)
				meb = q/za;
			else
				break;
		}
	}
	if(me < a)
		za = meb;
	else
		za = me;
	lli k = za - a;
	printf("%lld\n",k);
	return 0;
}

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

typedef long long int lli;

#define TRUE         (1==1)
#define FALSE        (!TRUE)

#define fo3(a,b,c)   for(a=(int)(b);a<=(int)(c);a++)
#define of3(a,b,c)   for(a=(int)(b);a>=(int)(c);a--)
#define in32(a)      scanf("%d",&(a))
#define in64(a)      scanf("%I64d",&(a))
#define out32(a)     printf("%d\n",(a))
#define out64(a)     printf("%I64d\n",(a))
#define instr(a)     scanf("%s",(a))
#define outstr(a)    printf("%s\n",(a))
#define max(a,b)     (((a)<(b))?(b):(a))
#define min(a,b)     (((a)>(b))?(b):(a))

lli gcd(lli a, lli b) {
    return (b == 0) ? a : gcd(b, a % b);
}
lli lcm(lli a, lli b) {
    lli g = gcd(a, b);
    return a * (b / g);
}

int facts(lli val, lli * arr, int st) {
    if (val == 1) {
        arr[0] = 1;
        return 1;
    }
    int i = st,k;
    while (i * i <= val) {
        if (val % i == 0) {
            int pow = 0;
            while (val % i == 0) {
                pow ++;
                val/=i;
            }
            int cur = facts(val,arr,i+1);
            int res = cur * (pow + 1);
            fo3(k,cur,res-1)
                arr[k] = arr[k-cur]*i;
            return res;
        }
        i++;
    }
    arr[0] = 1;
    arr[1] = val;
    return 2;
}

int main(void)
{
    lli x, y;
    int i;
    in64(x);
    in64(y);
    if (x < y) {
        x^=y;
        y^=x;
        x^=y;
    }
    lli diff = x - y;
    if (x == y) {
        out64(0);
        return EXIT_SUCCESS;
    }
    lli ans = 0;
    lli best = lcm(x, y);

    lli arr[2000];
    int num = facts(diff, arr, 2);
    fo3(i,0,num-1) {
        lli m = x % arr[i];
        if (m==0)
            m = diff;
        lli add = diff - m;
        add %= arr[i];
        lli tmp = lcm(x+add, y+add);
        if (tmp < best || (tmp == best && add < ans)){
            best = tmp;
            ans = add;
        }
    }
    out64(ans);
    return EXIT_SUCCESS;
}

#include<stdio.h>
#include<string.h>
#define MAX 100000
/*int temp[MAX];
void mergesort(int a[],int b[],int indices[],int first,int last)
{
    if(first==last) return;
    int middle=(first+last)/2;
    mergesort(a,b,indices,first,middle);
    mergesort(a,b,indices,middle+1,last);
    int i,j,k;
    for(i=first,j=first,k=middle+1;i<=last;i++)
    {
        if(j==middle+1) temp[i]=indices[k++];
        else if(k==last+1) temp[i]=indices[j++];
        else if(a[indices[k]]-b[indices[k]]>a[indices[j]]-b[indices[j]]) temp[i]=indices[k++];
        else temp[i]=indices[j++];
    }
    for(i=first;i<=last;i++)
    {
        indices[i]=temp[i];
    }
}*/
long long int gcd(long long int a,long long int b)
{
    if(b==0) return a;
    else return gcd(b,a%b);
}
int main()
{
    long long int a,b;
    scanf("%I64d %I64d",&a,&b);
    if(a>b)
    {
        int c=a;
        a=b;
        b=c;
    }
    long long p=a;
    long long int k=0,k1=0,compliment=b-a,compliment1=b-a;
    p=p*b;
    long long int GCD=gcd(a,b);
    p=p/GCD;
    long long int p2=p;
    for(k=1;k*k<=b-a;k++)
    {
        if((k-a%k!=k-b%k)||((b-a)%k)) continue;
        compliment=(b-a)/k;
        long long int G=gcd(a+(k-a%k)%k,b+(k-b%k)%k);
        long long int s=a+(k-a%k)%k;
        s=s*(b+(k-b%k)%k);
        s=s/G;
        if(s<p)
        {
            p=s;
            k1=(k-a%k)%k;
        }
        if(a%compliment!=b%compliment) continue;
        G=gcd(a+(compliment-a%compliment)%compliment,b+(compliment-b%compliment)%compliment);
        s=a+(compliment-a%compliment)%compliment;
        s=s*(b+(compliment-b%compliment)%compliment)/G;
        if(s<=p2)
        {
            p2=s;
            compliment1=(compliment-b%compliment)%compliment;
        }
    }
    if(p2<p)
    {
        k1=compliment1;
    }
    printf("%I64d",k1);
    return 0;
}

#include <stdio.h>

int main() {
	int a, b, c, x;
	long long k, k_, min, ab;

	scanf("%d%d", &a, &b);
	if (a == b) {
		printf("0\n");
		return 0;
	}
	if (a > b)
		c = a, a = b, b = c;
	min = (long long) a * b, k_ = 0;
	for (c = 1; c * c <= b - a; c++)
		if ((b - a) % c == 0) {
			x = c;
			k = (x - a % x) % x;
			ab = (a + k) * (b + k) / x;
			if (min > ab || (min == ab && k_ > k))
				min = ab, k_ = k;
			x = (b - a) / c;
			k = (x - a % x) % x;
			ab = (a + k) * (b + k) / x;
			if (min > ab || (min == ab && k_ > k))
				min = ab, k_ = k;
		}
	printf("%lld\n", k_);
	return 0;
}

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

typedef int64_t I64;

I64 greatestCommonDivisor(I64 a, I64 b);

int main () {

    I64 a, b;
    scanf("%"SCNi64, &a);
    scanf("%"SCNi64, &b);

    if (a == b) {
        printf("0\n");
        return 0;
    }

    if (a > b) {
        I64 t = b;
        b = a;
        a = t;
    }

    I64 kMin = 0, diff = b - a;
    I64 min = (a / greatestCommonDivisor(a, b) * b);

    for (I64 i = 1; i * i <= diff; ++i) {
        if (diff % i != 0) {
            continue;
        }

        I64 n = i;
        if (a % n != 0) {
            I64 k = n - a % n;
            I64 m = ((a + k) / greatestCommonDivisor(a + k, b + k) * (b + k));
            if ((m == min && k < kMin) || m < min) {
                min = m;
                kMin = k;
            }
        }

        n = diff / i;
        if (a % n != 0) {
            I64 k = n - a % n;
            I64 m = ((a + k) / greatestCommonDivisor(a + k, b + k) * (b + k));
            if ((m == min && k < kMin) || m < min) {
                min = m;
                kMin = k;
            }
        }
    }

    printf("%"PRIi64"\n", kMin);
    return 0;
}

I64 greatestCommonDivisor(I64 a, I64 b) {
    I64 res = a % b;
    while (res > 0) {
        a = b;
        b = res;
        res = a % b;
    }
    return b;
}

#include <stdio.h>
#include <string.h>
#include <ctype.h>
//#include <DEBUG.h>
#ifndef LL
#define LL long long
#endif
#ifndef uLL
#define uLL unsigned long long
#endif
#ifndef uL
#define uL unsigned int
#endif
#ifndef iL
#define iL int
#endif
#ifndef iS
#define iS short
#endif
#ifndef uS
#define uS unsigned short
#endif
#ifndef uC
#define uC unsigned char
#endif
#define max(a,b) (((a)>(b))?(a):(b))
#define min(a,b) (((a)<(b))?(a):(b))
#define abs(a) (((a)<0)?(-(a)):(a))
#define sgn(a) (((a)<0)?(1):(0))
#define movs(a,b,l) (memmove((a),(b),sizeof(*(b))*(l)))
#define stosb(a,b,l) (memset((a),(b),sizeof(*(a))*(l)))
static inline uL bsr(uL a)
{
    __asm__("bsrl %0,%0":"+r"((uL)a));
    return a;
}
static inline uLL sqrt64(uLL x)
{
    uLL t,g;
    for (g=(x>>1)+(x&1),t=x;g<t;g=(g+x/g)>>1)
    {
        t=g;
    }
    return t;
}
static inline uLL gcd(uLL a,uLL b)
{
    while (a>0)
    {
        b=(b%a)^a;
        a=a^b;
        b=a^b;
    }
    return b;
}
uLL prime[4000001];
uLL EulerSieve(uLL n)
{
    uLL i,j;
    memset(prime,0,sizeof(*prime)*(n+1));
    *prime=0;
    for (i=2;i<=n;i++)
    {
        if (prime[i]==0)
        {
            prime[++(*prime)]=i;
            prime[i]=i;
        }
        for (j=1;j<=*prime;j++)
        {
            if (i*prime[j]>n) {break;}
            prime[i*prime[j]]=prime[j];
            if (prime[i]==prime[j]) {break;}
        }
    }
    return *prime;
}
uLL fac[10000];
uLL times[10000];
uLL factoring(uLL a,uLL *fac,uLL *times)
{
	uLL k,i,l;
	l=sqrt64(a);
	for (k=0,i=1;(i<=prime[0])&&(prime[i]<=l);i++)
	{
		if (a%prime[i]==0)
		{
			fac[++k]=prime[i];
			times[k]=1;
			for (a=a/prime[i];a%prime[i]==0;a=a/prime[i],times[k]+=1);
		}
	}
	if (a>1)
	{
		fac[++k]=a;
		times[k]=1;
	}
	return k;
}
LL aa,bb,kk,ans1,ans2;
LL dfs(LL x,LL id)
{
	LL i,j,y;
	if (id>*fac)
	{
		kk=x-aa%x;
		if (x-bb%x==kk)
		{
			//printf("%lld %lld\n",x,kk);
			if ((aa+kk)/x*(bb+kk)<ans1)
			{
				ans1=(aa+kk)/x*(bb+kk);
				ans2=kk;
			}
		}
	}
	else
	{
		y=fac[id];
		for (i=0,j=1;i<=times[id];i++,j=j*fac[id])
		{
			dfs(x*j,id+1);
		}
	}
}
LL ans[100000];
int main()
{
	//freopen("in.in","r",stdin);
	LL i,j,k,a,b,c,n,m,x,y;
	EulerSieve(100000);
	scanf("%lld%lld",&a,&b);
	if (a>b) {c=a;a=b;b=c;}
	if (b%a==0)
	{
		printf("0\n");
	}
	else
	{
		ans1=a/gcd(a,b)*b;ans2=0;
		*fac=factoring(b-a,fac,times);
		aa=a;bb=b;
		dfs(1,1);
		printf("%lld\n",ans2);
	}
	return 0;
}

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

typedef int64_t i64;
typedef int32_t i32;

static void print_int(i64 n){if(n<0){putchar('-');n=-n;}if(n==0){putchar('0');return;}int s[20],len=0;while(n>0){s[len++]=n%10+'0';n/=10;}while(len>0){putchar(s[--len]);}}
static i64 read_int(void){int prev='\0';int c=getchar();while(!('0'<=c && c<='9')){prev=c;c=getchar();}i64 res=0;while('0'<=c && c<='9'){res=10*res+c-'0';c=getchar();}return prev=='-'?-res:res;}

i64 gcd (i64 a, i64 b) {
  i64 r = a % b;
  while (r > 0) {
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}

void run (void) {
  i32 a = read_int();
  i32 b = read_int();
  if (a == b) {
    puts ("0");
    return;
  }
  if (a > b) {
    i32 s = b;
    b = a;
    a = s;
  }
  i64 min = (i64)a / gcd (a, b) * b;
  i32 min_k = 0;
  i32 d = b - a;
  for (i32 i = 1; i * i <= d; ++i) {
    if (d % i != 0) continue;
    i32 f = i;
    if (a % f != 0) {
      i32 k = f - a % f;
      i64 v = (i64) (a + k) / gcd (a + k, b + k) * (b + k);
      if (v < min || (v == min && k < min_k)) {
	min = v;
	min_k = k;
      }
    }
    f = d / i;
    if (a % f != 0) {
      i32 k = f - a % f;
      i64 v = (i64) (a + k) / gcd (a + k, b + k) * (b + k);
      if (v < min || (v == min && k < min_k)) {
	min = v;
	min_k = k;
      }
    }
  }
  print_int (min_k);
  puts ("");
}

int main (void) {
  run ();
  return 0;
}

#include<stdio.h>
#include<math.h>
int main()
{
    int a,b,temp,c,d,flag=0,len,i;
    scanf("%d %d",&a,&b);
    if(a>b)
    {
        temp=a;
        a=b;
        b=temp;
    }
    if(a==b)
    {printf("0\n");return 0;}
    c=b-a;
    d=a%c;
    if(d==0)
    {printf("0\n");return 0;}
    if(a<c)
    {

        for(i=a;i<=c/2;i++)
        {
            if(c%i==0)
            {flag=1;break;}
        }
        if(flag)
        printf("%d\n",i-a);
        else
            printf("%d\n",c-a);
            return 0;
    }
    else
    {
        printf("%d\n",c-d);
        return 0;;
    }
}

/*			                       ____COMPETITIVE CODING____                                                          */

//--Header Files--

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

//--Macros--

#define N 200001
#define R 100000007
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))

//--Structures--

//--Comparator Functions--

int comp(const void *a, const void *b)
{
	return (*(int *)a - *(int *)b);
}
int comp_desc(const void *a, const void *b)
{
	return (*(int *)b - *(int *)a);
}

//--Function Declarations--

// void qsort(array name, size of array, size of each element, comp);
// int* bsearch(address of search element, array name, size of array, size of each element, comp);
int rmdup(int [], int); // int rmDup(array, size of array);
int gcd(int, int); // int gcd(first number, second number);

//--Useful Variables--

int i = 0, j = 0, k = 0, flag = 1, count = 0, temp, pos = 0, test_case, mx = 0, mn = INT_MAX;
long long sum = 0, product = 1;

int main()
{
	//clock_t tStart = clock();
	int a, b, diff;
	scanf("%d %d", &a, &b);
	diff = abs(a - b);
	mn = MIN(a, b);
	mx = MAX(a, b);
	if(mn == mx)
	{
		printf("0\n");
		return 0;
	}
	if(mn % diff == 0)
	{
		printf("0\n");
		return 0;
	}
	if(mn < diff)
	{
		for(i = mn; i <= diff / 2; i++)
		{
			if(diff % i == 0)
			{
				flag = 0;
				break;
			}
		}
		if(!flag)
		{
			printf("%d\n", i - mn);
			return 0;
		}
		else
		{
			printf("%d\n", diff - mn);
			return 0;
		}
	}
	else
	{
		printf("%d\n", diff - mn % diff);
		return 0;
	}

	//printf("%lf seconds.\n", (clock() - tStart)/CLOCKS_PER_SEC);
	return 0;
}

int rmdup(int a[], int n)
{
	if(n == 0 || n == 1)
		return n;
	int j = 0;
	for(i = 0; i < n - 1; i++)
	{
		if(a[i] != a[i+1])
		{
			a[j++] = a[i];
		}
	}
	a[j++] = a[n-1];
	return j;
}

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

#include <stdio.h>
#include <math.h>
typedef long long LL;

LL gcd(LL x, LL y)
{
	LL t;
	while(y){
		t = x;
		x = y;
		y = t % y;
	}
	return x;
}

int main()
{
	int a, b, d, k, i, c = 0;
	int dv[30000];
	LL x, y, t, min = 0, ans, z, ta;

	scanf("%d%d", &a, &b);
	if(a > b) a ^= b, b ^= a, a ^= b;
	d = b - a;
	if(!d){
		puts("0");
		return 0;
	}
	k = sqrt(d);
	for(i = 1; i <= k; i++){
		if(d % i == 0) dv[c++] = i;
	}
	i = c - 1;
	if(d == dv[i] * dv[i]) i--;
	for( ; i >= 0; i--){
		dv[c++] = d / dv[i];
	}
	for(i = 0; i < c; i++){
		t = (LL)dv[i];
		x = (LL)a / t * t;
		if(x < a) x += t;
		y = x + d;
		while(gcd(x / t, y / t) != 1) x += t, y += t;
		if(!min) ans = x - a, min = x / t * y ;
		else{
			z = x / t * y , ta = x - a;
			if(z < min) min = z, ans = ta;
			else if(z == min){
				if(ta < ans) ans = ta;
			}
		}
	}
	printf("%lld\n", ans);

	return 0;
}

#include<stdio.h>
int gcd(int a,int b){
	int t;
	while(b>0){
		t=a%b;
		a=b;
		b=t;
	}
	return a;
}
int main()
{
	long long a,b;
	scanf("%I64d %I64d",&a,&b);
	long long t;
	if(a>b){
		t=a;
		a=b;
		b=t;
	}
	t=b-a;
	long long k=0;
	long long k1;
	long long min=a/gcd(a,b)*b;
	for(long long i=1;i*i<=t;i++){
		k1=i-b%i;
		if((a+k1)*(b+k1)<min*gcd(a+k1,b+k1)){
			min=(a+k1)/gcd(a+k1,b+k1)*(b+k1);
			k=k1;
		}
		k1=(t/i)-b%(t/i);
		if((a+k1)/gcd(a+k1,b+k1)*(b+k1)<min){
			min=(a+k1)/gcd(a+k1,b+k1)*(b+k1);
			k=k1;
		}
	}
	printf("%I64d\n",k);
	return 0;
}

#include <stdio.h>
#define lli long long int
int main(void){
        lli a,b;
        scanf("%lld %lld",&a,&b);
        if(a > b){
                int ch = a;
                a = b;
                b = ch;
        }
        lli q = b - a;
        lli j = 0;
        while(q*(++j) < a);
        lli k = q*j - a;
        printf("%lld\n",k);
        return 0;
}

#include <stdio.h>
#define lli long long int
double sqrt(double i){
        double a = 2.0;
        int k = 50;
        while(k-->0){
                a = (a+i/a)/2.0;
        }
        return a;
}
int main(void){
        lli a,b;
        scanf("%lld %lld",&a,&b);
        if(a == b){
                printf("0\n");
                return 0;
        }
        if(a > b){
                int ch = a;
                a = b;
                b = ch;
        }
        lli q = b - a;
        lli j = a/q;
        if(q*j < a)
                j++;
        q *=j;
        int za = 1;
        int rq = sqrt(q);
        int me = 1;
        int meb = q;
        while((++za) <rq+1){
                if(q%(za) == 0){
                        me = za;
                        if(za >= a)
                                break;
                        if((q/za)>=a)
                                meb = q/za;
                        else
                                break;
                }
        }
        if(me < a)
                za = meb;
        else
                za = me;
        lli k = za - a;
        printf("%lld\n",k);
        return 0;
}

#include <stdio.h>
#define lli long long int
double sqrt(double i){
        double a = 2.0;
        int k = 100;
        while(k-->0){
                a = (a+i/a)/2.0;
        }
        return a;
}
int main(void){
        lli a,b;
        scanf("%lld %lld",&a,&b);
        if(a == b){
                printf("0\n");
                return 0;
        }
        if(a > b){
                int ch = a;
                a = b;
                b = ch;
        }
        lli q = b - a;
        lli j = a/q;
        if(q*j < a)
                j++;
        q *=j;
        int za = 1;
        int rq = sqrt(q);
        int me = 1;
        int meb = q;
        while((++za) <rq+1){
                if(q%(za) == 0){
                        me = za;
                        if(za >= a)
                                break;
                        if((q/za)>=a)
                                meb = q/za;
                        else
                                break;
                }
        }
        if(me < a)
                za = meb;
        else
                za = me;
        lli k = za - a;
        printf("%lld\n",k);
        return 0;
}

#include <stdio.h>
#define lli long long int
int main(void){
        lli a,b;
        scanf("%lld %lld",&a,&b);
        if(a == b){
                printf("%lld\n",a);
                return 0;
        }
        if(a > b){
                int ch = a;
                a = b;
                b = ch;
        }
        lli q = b - a;
        lli j = a/q;
        if(q*j < a)
                j++;
        q *=j;
        int za = a-1;
        while(q%(++za) != 0);
        lli k = za - a;
        printf("%lld\n",k);
        return 0;
}

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

typedef long long int lli;

#define TRUE         (1==1)
#define FALSE        (!TRUE)

#define fo3(a,b,c)   for(a=(int)(b);a<=(int)(c);a++)
#define of3(a,b,c)   for(a=(int)(b);a>=(int)(c);a--)
#define in32(a)      scanf("%d",&(a))
#define in64(a)      scanf("%I64d",&(a))
#define out32(a)     printf("%d\n",(a))
#define out64(a)     printf("%I64d\n",(a))
#define instr(a)     scanf("%s",(a))
#define outstr(a)    printf("%s\n",(a))
#define max(a,b)     (((a)<(b))?(b):(a))
#define min(a,b)     (((a)>(b))?(b):(a))

lli gcd(lli a, lli b) {
    return (b == 0) ? a : gcd(b, a % b);
}
lli lcm(lli a, lli b) {
    lli g = gcd(a, b);
    return a * (b / g);
}

int facts(lli val, lli * arr, int st) {
    if (val == 1) {
        arr[0] = 1;
        return 1;
    }
    int i = st,k;
    while (i * i <= val) {
        if (val % i == 0) {
            int pow = 0;
            while (val % i == 0) {
                pow ++;
                val/=i;
            }
            int cur = facts(val,arr,i+1);
            int res = cur * (pow + 1);
            fo3(k,cur,res-1)
                arr[k] = arr[k-cur]*i;
            return res;
        }
        i++;
    }
    arr[0] = 1;
    arr[1] = val;
    return 2;
}

int main(void)
{
    lli x, y;
    int i;
    in64(x);
    in64(y);
    if (x < y) {
        x^=y;
        y^=x;
        x^=y;
    }
    lli diff = x - y;
    if (x == y) {
        out64(0);
        return EXIT_SUCCESS;
    }
    lli ans = 0;
    lli best = lcm(x, y);

    lli arr[2000];
    int num = facts(diff, arr, 2);
    fo3(i,0,num-1) {
        lli m = x % arr[i];
        if (m==0)
            m = diff;
        lli add = diff - m;
        lli tmp = lcm(x+add, y+add);
        if (tmp < best || (tmp == best && add < ans)){
            best = tmp;
            ans = add;
        }
    }
    out64(ans);
    return EXIT_SUCCESS;
}

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

typedef long long int lli;

#define TRUE         (1==1)
#define FALSE        (!TRUE)

#define fo3(a,b,c)   for(a=(int)(b);a<=(int)(c);a++)
#define of3(a,b,c)   for(a=(int)(b);a>=(int)(c);a--)
#define in32(a)      scanf("%d",&(a))
#define in64(a)      scanf("%I64d",&(a))
#define out32(a)     printf("%d\n",(a))
#define out64(a)     printf("%I64d\n",(a))
#define instr(a)     scanf("%s",(a))
#define outstr(a)    printf("%s\n",(a))
#define max(a,b)     (((a)<(b))?(b):(a))
#define min(a,b)     (((a)>(b))?(b):(a))

int main(void)
{
    lli x, y;
    in64(x);
    in64(y);
    if (x < y) {
        x^=y;
        y^=x;
        x^=y;
    }
    lli diff = x - y;
    if (diff == 0 || y == 1) {
        out64(x*0);
        return EXIT_SUCCESS;
    }
    lli m = x % diff;
    if(m==0)
        m = diff;
    out64(diff-m);
    return EXIT_SUCCESS;
}

#include<stdio.h>
#include<string.h>
#define MAX 100000
/*int temp[MAX];
void mergesort(int a[],int b[],int indices[],int first,int last)
{
    if(first==last) return;
    int middle=(first+last)/2;
    mergesort(a,b,indices,first,middle);
    mergesort(a,b,indices,middle+1,last);
    int i,j,k;
    for(i=first,j=first,k=middle+1;i<=last;i++)
    {
        if(j==middle+1) temp[i]=indices[k++];
        else if(k==last+1) temp[i]=indices[j++];
        else if(a[indices[k]]-b[indices[k]]>a[indices[j]]-b[indices[j]]) temp[i]=indices[k++];
        else temp[i]=indices[j++];
    }
    for(i=first;i<=last;i++)
    {
        indices[i]=temp[i];
    }
}*/
int main()
{
    int a,b;
    scanf("%d %d",&a,&b);
    if(a>b)
    {
        int c=a;
        a=b;
        b=c;
    }
    if(b%a==0) printf("0");
    else if(b/a>1)
    {
        int c=(b/a)-1;
        int d=(b/a);
        for(d=(b/a);d>1;d--)
        {
            int q=(b-a*d)/(d-1);
            if(q>0)
            {
                printf("%d",q);
                return 0;
            }
        }
    }
    else
    {
        int p=a/(b-a)+(a%(b-a)>0);
        //printf("p=%d\n",p);
        long long int k=(b-a);
        //printf("k=%I64d\n",k);
        k=k*p;
        //printf("k=%I64d\n",k);
        k=k-a;
        //printf("k=%I64d\n",k);
        printf("%I64d",k);

    }
    return 0;
}

#include<stdio.h>
#include<string.h>
#define MAX 100000
/*int temp[MAX];
void mergesort(int a[],int b[],int indices[],int first,int last)
{
    if(first==last) return;
    int middle=(first+last)/2;
    mergesort(a,b,indices,first,middle);
    mergesort(a,b,indices,middle+1,last);
    int i,j,k;
    for(i=first,j=first,k=middle+1;i<=last;i++)
    {
        if(j==middle+1) temp[i]=indices[k++];
        else if(k==last+1) temp[i]=indices[j++];
        else if(a[indices[k]]-b[indices[k]]>a[indices[j]]-b[indices[j]]) temp[i]=indices[k++];
        else temp[i]=indices[j++];
    }
    for(i=first;i<=last;i++)
    {
        indices[i]=temp[i];
    }
}*/
int main()
{
    int a,b;
    scanf("%d %d",&a,&b);
    if(a>b)
    {
        int c=a;
        a=b;
        b=c;
    }
    if(b/a>1)
    {
        int c=(b/a)-1;
        int d=(b/a);
        for(d=(b/a);d>1;d--)
        {
            int q=(b-a*d)/(d-1);
            if(q>0)
            {
                printf("%d",q);
                return 0;
            }
        }
    }
    else
    {
        if(b==a)
        {
            printf("0");
        }
        else
        {
            int p=a/(b-a)+(a%(b-a)>0);
            //printf("p=%d\n",p);
            long long int k=(b-a);
            //printf("k=%I64d\n",k);
            k=k*p;
            //printf("k=%I64d\n",k);
            k=k-a;
            //printf("k=%I64d\n",k);
            printf("%I64d",k);
        }
    }
    return 0;
}

#include <stdio.h>

int main() {
	int a, b, c, x;
	long long k, k_, min, ab;

	scanf("%d%d", &a, &b);
	if (a == b) {
		printf("%d\n", a);
		return 0;
	}
	if (a > b)
		c = a, a = b, b = c;
	min = (long long) a * b, k_ = 0;
	for (c = 1; c * c <= b - a; c++)
		if ((b - a) % c == 0) {
			x = c;
			k = (x - a % x) % x;
			ab = (a + k) * (b + k) / x;
			if (min > ab || (min == ab && k_ > k))
				min = ab, k_ = k;
			x = (b - a) / c;
			k = (x - a % x) % x;
			ab = (a + k) * (b + k) / x;
			if (min > ab || (min == ab && k_ > k))
				min = ab, k_ = k;
		}
	printf("%lld\n", k_);
	return 0;
}

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

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

#include<stdio.h>
int gcd(int a,int b){
	int t;
	while(b>0){
		t=a%b;
		a=b;
		b=t;
	}
	return a;
}
int main()
{
	long long a,b;
	scanf("%I64d %I64d",&a,&b);
	long long t;
	if(a>b){
		t=a;
		a=b;
		b=t;
	}
	t=b-a;
	long long k=0;
	long long k1;
	long long min=a*b/gcd(a,b);
	for(long long i=1;i*i<=t;i++){
		k1=i-b%i;
		if((a+k1)*(b+k1)<min*gcd(a+k1,b+k1)){
			min=(a+k1)*(b+k1)/gcd(a+k1,b+k1);
			k=k1;
		}
		k1=(t/i)-b%(t/i);
		if((a+k1)*(b+k1)<min*gcd(a+k1,b+k1)){
			min=(a+k1)*(b+k1)/gcd(a+k1,b+k1);
			k=k1;
		}
	}
	printf("%I64d\n",k);
	return 0;
}

#include<stdio.h>
int gcd(int a,int b){
	int t;
	while(b>0){
		t=a%b;
		a=b;
		b=t;
	}
	return a;
}
int main()
{
	int a,b;
	scanf("%d %d",&a,&b);
	int t;
	if(a>b){
		t=a;
		a=b;
		b=t;
	}
	t=b-a;
	int k=0;
	int k1;
	int min=a*b/gcd(a,b);
	for(int i=1;i*i<=t;i++){
		k1=i-a%i;
		if((a+k1)*(b+k1)<min*gcd(a+k1,b+k1)){
			min=(a+k1)*(b+k1)/gcd(a+k1,b+k1);
			k=k1;
		}
		k1=(t/i)-a%(t/i);
		if((a+k1)*(b+k1)<min*gcd(a+k1,b+k1)){
			min=(a+k1)*(b+k1)/gcd(a+k1,b+k1);
			k=k1;
		}
	}
	printf("%d\n",k);
	return 0;
}