Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches.First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours, and each hour in m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation.Note that to display number 0 section of the watches is required to have at least one place.Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number.

The first line of the input contains two integers, given in the decimal notation, n and m (1 ≤ n, m ≤ 109) — the number of hours in one day and the number of minutes in one hour, respectively.

Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct.

NoteIn the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2).In the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1).

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

typedef long long ll;

int main(int argc, char ** argv){

	ll MAX = 823543;
	ll n,m;
	scanf("%lld%lld",&n,&m);
	if(n>MAX || m>MAX){
		printf("0\n");
		return 0;
	}
	ll num_dg_hr = 1;
	ll num_dg_min = 1;
	ll x1 = 7*num_dg_hr;
	ll x2 = 7*num_dg_min;
	while((n-1)/x1!=0){
		num_dg_hr++;
		x1*=7;
	}
	while((m-1)/x2!=0){
		num_dg_min++;
		x2*=7;
	}
	if(num_dg_min+num_dg_hr>7){
		printf("0\n");
		return 0;
	}
	ll i,j;
	ll ans = 0;
	for(i=0; i<n*m; i++){
		ll hr = i/m;
		ll min = i%m;
		ll * time = (ll*)calloc(sizeof(ll),(num_dg_min+num_dg_hr));
		for(j=0; j<num_dg_min; j++){
			time[j] = min%7;
			min = min/7;
		}
		for(j=num_dg_min; j<num_dg_min+num_dg_hr; j++){
			time[j] = hr%7;
			hr = hr/7;
		}
		ll * cnt = (ll*)calloc(sizeof(ll),7);
		for(j=0; j<num_dg_min+num_dg_hr; j++){
			cnt[time[j]]++;
		}
		bool flag = true;
		for(j=0; j<7; j++){
			if(cnt[j]>1){
				flag = false;
			}
		}
		if(flag){
			ans++;
		}
	}
	printf("%lld\n",ans);

	return 0;
}

#include <stdio.h>

long digitsIn7(long a)
{
	long r = 0;

	do
	{
		r++;
		a /= 7;
	} while (a);

	return r;
}

int hasDistinct(long v, long r)
{
	int mask = 0;

	while (r--)
	{
		long d = v % 7;
		if (mask & (1<<d)) return 0;
		mask |= 1 << d;
		v /= 7;
	}

	return 1;
}

int main()
{
	long a, b;
	scanf("%ld%ld", &a, &b);

	long da = digitsIn7(a-1);
	long db = digitsIn7(b-1);

	if (da + db > 7)
		puts("0");
	else
	{
		long pow = 1;
		long sum = 0;
		for (long i = 0; i < da; i++)
			pow *= 7;

		long i, j;

		for (i = 0; i < a; i++)
			for (j = 0; j < b; j++)
				if (hasDistinct(i + j*pow, da+db)) sum++;

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

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define MAXN 13
long long a[MAXN],b[MAXN],m,n,ans=0;
long long s1[MAXN],s2[MAXN];
void find(long long x,long long y,long long add)
{
	long long i,j,S;
	if(add>y){
		ans++;
		return;
	}
	for(i=0;i<=6;i++){
		for(j=1;j<add;j++)
			if(i==b[j])
				goto end1;
		for(j=1;j<=x;j++)
			if(i==a[j])
				goto end1;
		s2[add]=i*pow(7,y-add);
		S=0;
		for(j=1;j<=add;j++)
			S+=s2[j];
		if(S>=m)
			goto end1;
		b[add]=i;
		find(x,y,add+1);
		end1:;
	}
}
void dfs(long long x,long long y,long long add)
{
	long long i,j,S;
	if(add>x){
		find(x,y,1);
		return;
	}
	for(i=0;i<=6;i++){
		for(j=1;j<add;j++){
			if(i==a[j])
			goto end;
		}
		s1[add]=i*pow(7,x-add);
		S=0;
		for(j=1;j<=add;j++)
			S+=s1[j];
		if(S>=n)
			goto end;
		a[add]=i;
		dfs(x,y,add+1);
		end:;
	}
}
int main()
{
	long long k,i,j,l,c,p,q,c1=0,c2=0;
	scanf("%I64d%I64d",&n,&m);
	p=n-1;q=m-1;
	while(p>0){
		p=p/7;
		c1++;
	}
	while(q>0){
		q=q/7;
		c2++;
	}
	if(c1==0)
	c1++;
	if(c2==0)
		c2++;
	if(c1+c2>7){
		printf("0");
		return 0;
	}
	dfs(c1,c2,1);
	printf("%I64d",ans);
	return 0;
}

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
int cmp[250];
int cnt[10];
int cmp1[250];
int calc(int a){
	int cntx=0;
	while(a){
		a/=7;
		cntx++;
	}
	return cntx;
}
int insert(int x,int fix[]){
	if(x==0){
		fix[0]=0;
		return 1;
	}
	int i,k; i=k=0;
	while(x){
		fix[i++]=x%7;
		x/=7;
	}
	for(k=0;k<i/2;k++){
		int temp = fix[k];
		fix[k] = fix[i-k-1];
		fix[i-k-1]=temp;
	}

	return i;
}
int fill(int x,int fix[],int t){
	int i,k; i=k=0; int lock[250];
	for(i=0;i<x;i++){
		lock[i]=fix[i];
	}
	for(i=0;i<t-x;i++){
		fix[i]=0;
	}
	for(i=t-x;i<x+(t-x);i++){
		fix[i] = lock[k++];
	}
}
int same(int x,int y){
	int i;
	memset(cnt,0,sizeof(cnt));
	for(i=0;i<x;i++){
		cnt[cmp[i]]++;
	}
	for(i=0;i<y;i++){
		cnt[cmp1[i]]++;
	}
	for(i=0;i<7;i++){
		if(cnt[i]>1)
		return 0;
	}
	return 1;
}
int main(){
	int a,b,c,d,e,f,g,h;
	scanf("%d%d",&a,&b);
	int t = calc(a-1); int t1=calc(b-1);
	if(t+t1>7){
		printf("0");
		return 0;
	}
	int i; int cnt=0;
	for(i=0;i<a;i++){
		int val = insert(i,cmp);
		if(val<t){
			fill(val,cmp,t);
			val=val+(t-val);
		}
		for(c=0;c<b;c++){
			int val1 = insert(c,cmp1);
			if(val1<t1){
				fill(val1,cmp1,t1);
				val1=val1+(t1-val1);
			}
			if(same(val,val1)){
			cnt++;
			}
		}
	}
	printf("%d",cnt);
}

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
char cmp[250];
int cnt[10];
char cmp1[250];
void re_use(char temp[],int t){
	char new_arr[250];
	int len = strlen(temp); int i;
	for(i=0;i<t-len;i++){
		new_arr[i]='0';
	} int k=0;
	for(i=t-len;i<len+t;i++){
		new_arr[i] = temp[k++];
	}

	strcpy(temp,new_arr);
}
int calc(int a){
	int cntx=0;
	while(a){
		a/=7;
		cntx++;
	}
	return cntx;
}
int check(){
	int len = strlen(cmp); int len1=strlen(cmp1);
	int i;
	memset(cnt,0,sizeof(cnt));
	for(i=0;i<len;i++){
		cnt[cmp[i]-'0']++;
	}
	for(i=0;i<len1;i++){
		cnt[cmp1[i]-'0']++;
	}
	for(i=0;i<7;i++){
		if(cnt[i]>1)
			return 0;
	}
	return 1;
}
int main(){
	int a,b,c,d,e,f,g,h;
	scanf("%d%d",&a,&b);
	int t = calc(a-1); int t1=calc(b-1);
	if(t+t1>7){
		printf("0");
		return 0;
	}
	int i; int cnt=0;
	for(i=0;i<a;i++){
		itoa(i,cmp,7);
		if(strlen(cmp)<t){
			re_use(cmp,t);
		}
		for(c=0;c<b;c++){
			itoa(c,cmp1,7);
			if(strlen(cmp1)<t1){
				re_use(cmp1,t1);
			}
			if(check())
			cnt++;
		}
	}
	printf("%d",cnt);

}

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

#define SET(a,b) memset(a,b,sizeof(a))
#define scan(n) scanf("%d",&n)
#define print(n) printf("%d\n",n)
#define scanl(n) scanf("%lld",&n)
#define printl(n) printf("%lld\n",n)

//stacks
/*void push(int n,int ar[],int i)
  {
  ar[i]=n;
  i++;
  }
  void pop(int ar[],int i)
  {
  if(i>0)
  {
  ar[i-1]=0;
  i--;
  }
  }*/

//queues
/*void qpush(int ar[],int val,int head,int tail,int range)
  {
  if(head==tail)
  {
  ar[head]=val;
  if(tail+1==range)
  tail=0;
  else
  tail++;
  }
  else
  {
  ar[tail]=val;
  if(tail+1==range)
  tail=0;
  else
  tail++;
  }
  if(tail==head)
  {
  if(head+1==range)
  head=0;
  else
  head++;
  }
  }

  void qpop(int head,int tail,int range)
  {
  if(head+1==range)
  head=0;
  else
  head++;
  }*/

//pow
/*int fun(int a,int b,int m)
  {
  if(b==0)
  return 1;
  int d,x;
  d=fun(a,b/2,m);
  x=(d*d)%m;
  if(b%2==0)
  return x;
  else
  return (x*a%m)%m;
  }
  */
//bst
/*typedef struct tree
  {
  int val;
  struct tree *l;
  struct tree *r;
  }node;
//node *head;
node *insert(int x,node *root)
{
if(root==NULL)
{
root=(node *)malloc(sizeof(node));
root->val=x;
}
else
{
if(x<=root->val)
{
if(root->l==NULL)
{
root->l=(node *)malloc(sizeof(node));
root->l->val=x;
}
else
root->l=insert(x,root->l);
}
else
{
if(root->r==NULL)
{
root->r=(node *)malloc(sizeof(node));
root->r->val=x;
}
else
{
root->r=insert(x,root->r);
}
}
}
return root;
}
node *find(int x,node *root)
{
if(root==NULL)
{
return 0;
}
else
{
if(x==root->val)
return root;
else if(x<root->val)
{
if(root->l==NULL)
{
return 0;
}
else
{
find(x,root->l);
}
}
else
{
if(root->r==NULL)
{
return 0;
}
else
{
find(x,root->r);
}
}
}
}

void inorder(node *root)
{
       if(root==NULL)
       {
              return;
       }
       if(root->l==NULL)
       {
              printf("%d",root->val);
              if(root->r==NULL)
                     return;
              else
                     inorder(root->r);
       }
       else
       {
              inorder(root->l);
              printf("%d",root->val);
              if(root->r==NULL)
                     return;
              else
                     inorder(root->r);
       }
}
void preorder(node *root)
{
       if(root==NULL)
       {
              return;
       }
       else
       {
              printf("%d",root->val);
              preorder(root->l);
              preorder(root->r);
       }
}

void postorder(node *root)
{
       if(root==NULL)
       {
              return;
       }
       else
       {
              postorder(root->l);
              postorder(root->r);
              printf("%d",root->val);
       }
}
*/
//adjacency list
/*typedef struct ll
  {
  int val;
  struct ll *next;
  }nd;

//n *ar[100001];
void insert(int a,int b)
{
if(ar[a]==NULL)
{
ar[a]=(nd *)malloc(sizeof(nd));
ar[a]->val=b;
ar[a]->next=NULL;
}
else
{
nd *temp=(nd *)malloc(sizeof(nd)),*t;
temp->val=b;
temp->next=ar[a];
ar[a]=temp;
}
}

//dfs
//int state[100001];
void dfs(int a)
{
state[a]=1;
count++;
nd *temp=ar[a];
while(temp!=NULL)
{
if(state[temp->val]!=1)
dfs(temp->val);
temp=temp->next;
}
return;
}*/

//dijkstra + heaps
/*typedef struct ll2
  {
  int val;
  int weight;
  struct ll2 *next;
  }nde;
  */
//nde *ar[500005];
//int state[500005];

/*void insert(int a,int b,int w)//insert to adjacency list
  {
  if(ar[a]==NULL)
  {
  ar[a]=(node *)malloc(sizeof(node));
  ar[a]->val=b;
  ar[a]->weight=w;
  ar[a]->next=NULL;
  }
  else
  {
  node *temp=(node *)malloc(sizeof(node)),*t;
  temp->val=b;
  temp->weight=w;
  temp->next=ar[a];
  ar[a]=temp;
  }
  }*/

//int pt[500005],d[500005];
//int heap[500005][2];
//int ptr;
/*void decpty(int val,int pos)//decrease priority
  {
  heap[pos][0]=val;
  int x=pos;
  while(x>0)
  {
  if(heap[x][0]>=heap[x/2][0])
  break;
  else
  {
  pt[heap[x][1]]=x/2;
  pt[heap[x/2][1]]=x;
  int temp=heap[x][0],t2=heap[x][1];
  heap[x][0]=heap[x/2][0];
  heap[x][1]=heap[x/2][1];
  heap[x/2][0]=temp;
  heap[x/2][1]=t2;
  x=x/2;
  }
  }
  }

  void hinsert(int val,int val2)//insert to heap
  {
  heap[ptr][0]=val;
  heap[ptr][1]=val2;
  pt[val2]=ptr;
  int x=ptr;
  ptr++;
  while(x>0)
  {
  if(heap[x][0]>=heap[x/2][0])
  break;
  else
  {
  pt[heap[x][1]]=x/2;
  pt[heap[x/2][1]]=x;
  int temp=heap[x][0],t2=heap[x][1];
  heap[x][0]=heap[x/2][0];
  heap[x][1]=heap[x/2][1];
  heap[x/2][0]=temp;
  heap[x/2][1]=t2;
  x=x/2;
  }
  }
  }

  int hdeletemin()//delete min elem from heap
  {
  int ret;
  ret=heap[1][1];
  pt[heap[1][1]]=-1;
  pt[heap[ptr-1][1]]=1;
  heap[1][0]=heap[ptr-1][0];
  heap[1][1]=heap[ptr-1][1];
  ptr--;
  int x=1;
  while(x<ptr)
  {
  if(2*x+1>=ptr)
  {
  if(2*x<ptr)
  {
  if(heap[2*x][0]<heap[x][0])
  {
  pt[heap[x][1]]=2*x;
  pt[heap[2*x][1]]=x;
  int temp=heap[x][0];
  int temp2=heap[x][1];
  heap[x][0]=heap[2*x][0];
  heap[x][1]=heap[2*x][1];
heap[2*x][0]=temp;
heap[2*x][1]=temp2;
x=2*x;
}
else
break;
}
else
break;
}
else if(heap[x][0]<=heap[2*x][0] && heap[x][0]<=heap[2*x+1][0])
       break;
       else
{
       if(heap[2*x][0]<=heap[2*x+1][0])
       {
              pt[heap[x][1]]=2*x;
              pt[heap[2*x][1]]=x;
              int temp=heap[x][0];
              int temp2=heap[x][1];
              heap[x][0]=heap[2*x][0];
              heap[x][1]=heap[2*x][1];
              heap[2*x][0]=temp;
              heap[2*x][1]=temp2;
              x=2*x;
       }
       else
       {
              pt[heap[x][1]]=2*x+1;
              pt[heap[2*x+1][1]]=x;
              int temp=heap[x][0];
              int temp2=heap[x][1];
              heap[x][0]=heap[2*x+1][0];
              heap[x][1]=heap[2*x+1][1];
              heap[2*x+1][0]=temp;
              heap[2*x+1][1]=temp2;
              x=2*x+1;
       }
}
}
return ret;
}

void dikstra(int u,int v)
{
       while(ptr!=1)
       {
              int v=hdeletemin();
              node *temp=ar[v];
              while(temp!=NULL)
              {
                     int w=temp->val;
                     if(d[v]+temp->weight < d[w])
                     {
                            d[w]=d[v]+temp->weight;
                            decpty(d[w],pt[w]);
                     }
                     temp=temp->next;
              }
       }
}*/

/*int main()
  {
  int n,m,u,v,w,i;
  scanf("%d%d",&n,&m);
  for(i=0;i<=n;i++)
  state[i]=0;
  while(m--)
  {
  scanf("%d%d%d",&u,&v,&w);
  insert(u,v,w);
  }
  scanf("%d%d",&u,&v);
  ptr=1;
  hinsert(0,u);
  d[u]=0;
  for(i=1;i<=n;i++)
  {
  if(i!=u)
  {
  hinsert(1000000000,i);
  d[i]=1000000000;
  }
  }
  dikstra(u,v);
  if(d[v]==1000000000)
  printf("NO\n");
  else
  printf("%d\n",d[v]);
  return 0;
  }*/

//kruskal + dsu

//int dsu[N];
/*int find(int x)
  {
  if(dsu[x]==x)return x;
  return dsu[x] = find(dsu[x]);
  }

  void Union(int a,int b)
  {
  if(a==b)
  return;
  dsu[a] = b;
  }

  typedef struct edge
  {
  int u,v;
  int weight;
  }edge;
  */
//edge e[N];
/*int cmp(const void *a,const void *b)
  {
  return ((edge*)a)->weight - ((edge*)b)->weight;
  }*/
/*int main()
  {
  int i;
  int n,m;
  scanf("%d %d\n",&n,&m);
  for(i=0;i<m;i++)
  scanf("%d %d %d",&e[i].u,&e[i].v,&e[i].weight);
  for(i=1;i<=n;i++)
  dsu[i]=i;
  qsort(e,m,sizeof(edge),cmp);
  long long int sum=0;
  for(i=0;i<m;i++)
  {
  int a,b;
  if((a=find(e[i].u))!=(b=find(e[i].v)))
  {
  sum+=e[i].weight;
  Union(a,b);
  }
  }
  printf("%lld\n",sum);
  return 0;
  }*/

//max
/*int max(int a,int b)
  {
  if(a>b)
  return a;
  else
  return b;
  }
  */
//qsort

/*int cmpfunc(const void *a,const void *b)
  {
  return (*(int *)a - *(int *)b);
  }*/
//qsort(ar,n,sizeof(int),cmpfunc);

//-------------------------------------------------------------------------------------------------------------------------------------

int main()
{
       int n,m,ct,ans=0;
       scan(n);
       scan(m);
       int i,j,k,val=0;
       int dig1,dig2;
       val=(n-1)/7;
       dig1=1;
       dig2=1;
       while(val>0)
       {
              dig1++;
              val=val/7;
       }

       val=(m-1)/7;
       while(val>0)
       {
              dig2++;
              val=val/7;
       }
       val=dig1+dig2;
       if(val>7)
       {
              ans=0;
              print(ans);
              return 0;
       }
       int status[10],st2[10],cond=0;
       for(i=0;i<n;i++)
       {
              for(j=0;j<m;j++)
              {
                     cond=0;
                     int k=0;
                     for(k=0;k<10;k++)
                            status[k]=0;
                     int a,b,v1=i,v2=j;
                     for(a=0;a<dig1;a++)
                     {
                            if(status[v1%7]==1)
                            {
                                   cond=1;
                                   break;
                            }
                            else
                                   status[v1%7]=1;
                            v1=v1/7;
                     }

                     for(a=0;a<dig2;a++)
                     {
                            if(cond==1)
                                   break;
                            if(status[v2%7]==1)
                            {
                                   cond=1;
                                   break;
                            }
                            else
                                   status[v2%7]=1;
                            v2=v2/7;
                     }
                     if(cond==0)
                     {
                            ans++;
                     }
              }

       }
       print(ans);
       return 0;
}

#include<stdio.h>
#include<math.h>
int dgt(int a)
{
    int c=1;
    a--;
    while(a/7>0){
        a/=7;
        c++;
    }
    return c;
}
int ys(int m, int n, int a, int b)
{
    int fr[7]={0,0,0,0,0,0,0},i;
    //printf("%d-%d: ",m,n);
    for(i=0;i<a;i++){
        fr[m%7]++;
        if(fr[m%7]>1){
            //printf("0\n");
            return 0;
        }
        m=m/7;
    }
    for(i=0;i<b;i++){
        fr[n%7]++;
        if(fr[n%7]>1){
                //printf("0\n");
            return 0;
        }
        n=n/7;
    }
    //printf("1\n");
    return 1;
}
int get(int m, int n)
{
    int d1,d2,i,j,rs=0;
    d1=dgt(m);
    d2=dgt(n);
    //printf("D=%d,%d\n",d1,d2);
    if(d1+d2>7)
        return 0;
    for(i=0;i<m;i++){
        for(j=0;j<n;j++){
            rs+=ys(i,j,d1,d2);
        }
    }
    return rs;
}
int main()
{
    int m,n,result;
    scanf("%d %d",&m,&n);
    result=get(m,n);
    printf("%d\n",result);
    return 0;
}

#include <stdio.h>

int n, m;
int dign, digm;
int res;

int dig(int k) {
    int ans=0;
    if(k==0) return 1;
    while(k>0) {
        k/=7;
        ans++;
    }
    return ans;
}

int chk(int h, int m) {
    int visit[7];
    for(int i=0;i<7;i++) visit[i]=0;
    for(int i=0;i<dign;i++) {
        visit[h%7]++;
        h/=7;
    }
    for(int i=0;i<digm;i++) {
        visit[m%7]++;
        m/=7;
    }
    for(int i=0;i<7;i++) {
        if(visit[i]>1) return 0;
    }
    return 1;
}

int main() {
    int tmp;
    scanf("%d %d", &n, &m);
    dign=dig(n-1), digm=dig(m-1);
    if(dign+digm>7) puts("0");
    else {
        for(int i=0;i<n;i++) {
            for(int j=0;j<m;j++) {
                if(chk(i, j)==1) res++;
            }
        }
        printf("%d", res);
    }
    return 0;
}

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

int main() {
	int n, m, d;
	scanf("%d%d", &n, &m);
	int dn = 1, dm = 1;
	for(int k = 7 ; k < n ; k *= 7) dn++;
	for(int k = 7 ; k < m ; k *= 7) dm++;
	d = dn + dm;
	if(d > 7) {
		printf("0\n");
		return 0;
	}
	int r = 0;
	for(int i=0 ; i<n ; i++) {
		for(int j=0 ; j<m ; j++) {
			int u[7] = {0};
			int a = i, b = j;
			for(int k=0 ; k<dn ; k++) {
				u[a%7]++;
				a /= 7;
			}
			for(int k=0 ; k<dm ; k++) {
				u[b%7]++;
				b /= 7;
			}
			r++;
			for(int k=0 ; k<7 ; k++) {
				if(u[k] > 1) {
					r--;
					break;
				}
			}
		}
	}
	printf("%d\n", r);
	return 0;
}

#include <stdio.h>

int check(int a, int b, int c)
{
	int oc[7]={0}, sum=0,flag=0,i=0;

	while(a)
	{
		if(oc[a%7]) {flag=1;break;}
		else oc[a%7]++;
		a/=7;
		sum++;
	}

	while(b&&!flag)
	{
		if(oc[b%7]) {flag=1;break;}
		oc[b%7]++;
		b/=7;
		sum++;
	}

	if(c-sum>1) flag=1;

	if(c-sum>0 && oc[0]==1) flag=1;

	if(flag==1) return 0;

	else return 1;
}

int main(void) {
	int n,m,i=0,a,d=0,ans=0;

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

	a=n-1;
	while(1)
	{
		d++;
		a/=7;
		if(a==0) break;
	}

	a=m-1;
	while(1)
	{
		d++;
		a/=7;
		if(a==0) break;
	}

	if(d>7) printf("0");

	else
	{
		i=0;
		while(i<n)
		{
			int j=0;

			while(j<m)
			{
				ans+=check(i,j,d);
				j++;
			}

			i++;
		}

		printf("%d",ans);
	}

	return 0;
}

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

int arr[7];

int fn(int i,int j,int p,int q){
	int k,l;
	for(k=0;k<p;k++){
		arr[k]=i%7;
		i/=7;
	}
	for(k=p;k<p+q;k++){
		arr[k]=j%7;
		j/=7;
	}
	for(k=0;k<p+q;k++){
		for(l=k+1;l<p+q;l++){
			if(arr[l]==arr[k])return 0;
		}
	}
	return 1;
}

int fn1(int p,int q,int n,int m){
	int count=0,i,j;
	for(i=0;i<n;i++){
		for(j=0;j<m;j++){
			if(fn(i,j,p,q))count++;
		}
	}
	return count;
}

int main(){
	int n,nn,m,mm,p,q;
	scanf("%d%d",&n,&m);
	nn=n-1;
	mm=m-1;
	p=q=1;
	while(nn>6){
		p++;
		nn/=7;
	}
	while(mm>6){
		q++;
		mm/=7;
	}
	if(p+q>7)printf("0\n");
	else printf("%d\n",fn1(p,q,n,m));
	return 0;
}

#include <stdio.h>
#include <string.h>
#define NMAX 20
char hh[NMAX];
char mm[NMAX];
int xh[9];
short int fl[8];
int res;
int h, m;
int findans(int x, int ho, int mo)
{
	int i, th, tm;
	if (x > ho + mo)
	{
		th = 0;
		for (i = 0; i < ho; i++)
		{
			th = th * 7 + xh[i];
		}
		tm = 0;
		for (i = ho; i < ho + mo; i++)
		{
			tm = tm * 7 + xh[i];
		}
		if (th < h&&tm < m) res += 1;
	}
	else
	{
		for (i = 0; i < 7; i++)
		{
			if (fl[i] == 0)
			{
				xh[x - 1] = i;
				fl[i] = 1;
				findans(x + 1, ho, mo);
				fl[i] = 0;
			}
		}
	}
	return 0;
}
int main()
{
	int i, j, th, tm, d;
	int mo, ho;
	scanf("%d%d", &h, &m);
	th = h - 1;
	tm = m - 1;
	mo = ho = 0;
	while (tm > 0)
	{
		mm[mo] = tm % 7;
		tm = tm / 7;
		mo++;
	}
	if (mo == 0) mo = 1;
	while (th > 0)
	{
		hh[ho] = th % 7;
		ho++;
		th = th / 7;
	}
	if (ho == 0) ho = 1;
	if (ho + mo > 7) printf("0\n");
	else
	{
		memset(fl, 0, sizeof(fl));
		res = 0;
		findans(1, ho, mo);
		printf("%d\n", res);
	}
	return 0;
}

#ifdef ONLINE_JUDGE
    #define NDEBUG 1
#endif
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <limits.h>
#include <math.h>

#define long int64_t
#define fore(i,k,n) for (int _k = (k), _n = (n), i = _k; i <= _n; ++i)
#define forr(i,n,k) for (int _k = (k), _n = (n), i = _n; i >= _k; --i)
#define LIST_INIT_CAP 4
#define HASH_INIT_CAP 64
#define HASH_SKIP 11
#define TREE_INIT_CAP 1024
#define TREE_MAX_HEIGHT 64
#define DEQUE_INIT_CAP 1024
#define HEAP_INIT_CAP 1024
#define TAKEN 1
#define EMPTY 0
#define TOMB -1
#define max(a,b) (((a)>(b))?(a):(b))
#define min(a,b) (((a)<(b))?(a):(b))
#define abs(x) (((x) < 0) ? -(x) : (x))
#define N 1000001

bool set[10] = { 0, 0, 0, 0, 0, 0, 0, 1, 1, 1 };
int digits[20], tdigits[20];
int limit[8] = { 0, 6, 65, 654, 6543, 65432, 654321, 6543210 };

void addone(int idx) {
    ++tdigits[idx];
    if (tdigits[idx] > 9) {
        tdigits[idx] = 0;
        addone(idx - 1);
    }
}

int main() {
    int n, m;
    scanf("%d %d", &n, &m);
    --n; --m;
    if (n == 0 && m == 0) {
        printf("0");
        return 0;
    }
    int i = 20;
    while (m > 0) {
        digits[--i] = m % 7;
        m = m / 7;
    }
    int digm = 20 - i;
    if (digm == 0) {
        digm = 1;
        --i;
    }
    while (n > 0) {
        digits[--i] = n % 7;
        n = n / 7;
    }
    int dign = 20 - digm - i;
    if (dign == 0) dign = 1;
    int totdig = dign + digm;
    if (totdig > 7) {
        printf("0");
        return 0;
    }
    int answer = 0;
    fore (j, 0, limit[totdig]) {
        bool nok = false, mok = false;
        fore (k, 20 - totdig, 20 - (digm + 1)) {
            if (set[tdigits[k]]) goto cont;
            else set[tdigits[k]] = true;
            if (!nok && tdigits[k] > digits[k]) goto cont;
            if (!nok && tdigits[k] < digits[k]) nok = true;
        }
        fore (k, 20 - digm, 19) {
            if (set[tdigits[k]]) goto cont;
            else set[tdigits[k]] = true;
            if (!mok && tdigits[k] > digits[k]) goto cont;
            if (!mok && tdigits[k] < digits[k]) mok = true;
        }
        ++answer;
cont:
        addone(19);
        fore (k, 0, 6) set[k] = false;
    }

    printf("%d", answer);

    return 0;
}

#include <stdio.h>

int count(int n) {
	return n < 7 ? 1 : count(n / 7) + 1;
}

int check(int *ii, int ncnt, int mcnt, int n, int m) {
	int i;
	long long a, b;

	a = 0;
	for (i = 0; i < ncnt; i++)
		a = 7LL * a + ii[i];
	b = 0;
	for (i = 0; i < mcnt; i++)
		b = 7LL * b + ii[i + ncnt];
	return a < n && b < m;
}

int main() {
	int n, m, cnt, ncnt, mcnt, dcnt;
	static int ii[7], used[7];

	scanf("%d%d", &n, &m);
	ncnt = count(n - 1);
	mcnt = count(m - 1);
	dcnt = ncnt + mcnt;
	cnt = 0;
	for (ii[0] = 0; ii[0] < 7; ii[0]++) {
		if (used[ii[0]])
			continue;
		used[ii[0]] = 1;
		if (dcnt == 1 && check(ii, ncnt, mcnt, n, m))
			cnt++;
		for (ii[1] = 0; ii[1] < 7; ii[1]++) {
			if (used[ii[1]])
				continue;
			used[ii[1]] = 1;
			if (dcnt == 2 && check(ii, ncnt, mcnt, n, m))
				cnt++;
			for (ii[2] = 0; ii[2] < 7; ii[2]++) {
				if (used[ii[2]])
					continue;
				used[ii[2]] = 1;
				if (dcnt == 3 && check(ii, ncnt, mcnt, n, m))
					cnt++;
				for (ii[3] = 0; ii[3] < 7; ii[3]++) {
					if (used[ii[3]])
						continue;
					used[ii[3]] = 1;
					if (dcnt == 4 && check(ii, ncnt, mcnt, n, m))
						cnt++;
					for (ii[4] = 0; ii[4] < 7; ii[4]++) {
						if (used[ii[4]])
							continue;
						used[ii[4]] = 1;
						if (dcnt == 5 && check(ii, ncnt, mcnt, n, m))
							cnt++;
						for (ii[5] = 0; ii[5] < 7; ii[5]++) {
							if (used[ii[5]])
								continue;
							used[ii[5]] = 1;
							if (dcnt == 6 && check(ii, ncnt, mcnt, n, m))
								cnt++;
							for (ii[6] = 0; ii[6] < 7; ii[6]++) {
								if (used[ii[6]])
									continue;
								used[ii[6]] = 1;
								if (dcnt == 7 && check(ii, ncnt, mcnt, n, m))
									cnt++;
								used[ii[6]] = 0;
							}
							used[ii[5]] = 0;
						}
						used[ii[4]] = 0;
					}
					used[ii[3]] = 0;
				}
				used[ii[2]] = 0;
			}
			used[ii[1]] = 0;
		}
		used[ii[0]] = 0;
	}
	printf("%d\n", cnt);
	return 0;
}

#include <stdio.h>

int count(int n) {
	return n < 7 ? 1 : count(n / 7) + 1;
}

int power(int k) {
	return k == 0 ? 1 : 7 * power(k - 1);
}

int valid(int n, int k) {
	int used[7] = { 0 };

	while (k-- > 0) {
		int d = n % 7;

		if (used[d])
			return 0;
		used[d] = 1;
		n /= 7;
	}
	return 1;
}

int main() {
	int n, i, j, m, cnt, ncnt, mcnt, dcnt, p;

	scanf("%d%d", &n, &m);
	ncnt = count(n - 1);
	mcnt = count(m - 1);
	dcnt = ncnt + mcnt;
	p = power(mcnt);
	if (ncnt + mcnt > 7) {
		printf("0\n");
		return 0;
	}
	cnt = 0;
	for (i = 0; i < n; i++)
		for (j = 0; j < m; j++) {
			int x = i * p + j;

			if (valid(x, dcnt))
				cnt++;
		}
	printf("%d\n", cnt);
	return 0;
}

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

const int N = 24;

int
dec_num(int* d, int n){
  int rslt = d[0];
  int seven_plus  = 7;
  for(int i = 1; i < n; i++){
    rslt += d[i] * seven_plus;
    seven_plus *=7;
  }
  return rslt;
}

int
next(int* d, int n, int h_limit){
  if( dec_num(d,n) >= h_limit - 1 ){
    return 0;
  }else{
    for(int i= 0; i< n; i++){
      if(d[i] < 6){
        d[i]++;
        break;
      }else{
        d[i]=0;
      }
    }
  }
  return 1;
}

int
is_valid(int* d, int n){
  int flag[7];
  for(int i=0; i<7; i++) flag[i] =0;

  for(int i = 0; i < n; i++){
    int x = d[i];
    if(flag[x]){
      return 0;
    }else{
      flag[x]= 1;
    }
  }

  return 1;
}

int
is_valid_2(int* d1, int* d2, int n1, int n2){

  for(int i = 0; i < n1; i++){
    for(int j = 0; j < n2; j++){
      if(d1[i]==d2[j]) return 0;
    }
  }

//  for(int i = 0; i < n1; i++){
//    printf("=%d ", d1[i]);
//  }
//  printf("\n");
//
//  for(int i = 0; i < n2; i++){
//    printf("=%d ", d2[i]);
//  }
//  printf("\n");

  return 1;
}

int
main(){
  int hour[N];
  int minute[N];

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

  int ndd = ceil(log(n) / log(7) );
  int ndm = ceil(log(m) / log(7) );

  if(ndd == 0) ndd++;
  if(ndm == 0) ndm++;

  //Debug
  //printf("--%d %d\n",ndd,ndm);
  //return 0;

  if(ndd+ndm>7){
    printf("%d\n",0);
    return 0;
  }

  //Now ndd and ndm are really small, I will use brute force.
  int count = 0;

  for(int i=0; i< ndd; i++) hour[i] = 0;

  //Debug
  //int d[] = {6,6,6,6,6};
  //int x = dec_num(d,5);
  //printf("?? %d\n",x);

//  do {
//    int x = dec_num(hour,ndd);
//      for(int i=0; i< ndm; i++) minute[i] = 0;
//      do {
//        int y = dec_num(minute,ndm);
//        printf(">>%d %d\n", x,y);
//      }while( next(minute,ndm, m) );
//  }while( next(hour,ndd,n) );

  do{
    if( is_valid(hour,ndd) ){
      for(int i=0; i< ndm; i++) minute[i] = 0;
      do{
        if( is_valid(minute,ndm) ){
          if( is_valid_2(hour,minute, ndd,ndm) ){
            count++;
//            int x, y;
//            x = dec_num(hour,ndd);
//            y = dec_num(minute,ndm);
//            printf("--%d %d\n", x,y);
          }
        }
      }while( next(minute,ndm, m) );
    }
  }while( next(hour,ndd,n) );

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

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

const int N = 24;

int
dec_num(int* d, int n){
  int rslt = d[0];
  int seven_plus  = 7;
  for(int i = 1; i < n; i++){
    rslt += d[i] * seven_plus;
    seven_plus *=7;
  }
  return rslt;
}

int
next(int* d, int n, int h_limit){
  if( dec_num(d,n) >= h_limit - 1 ){
    return 0;
  }else{
    for(int i= 0; i< n; i++){
      if(d[i] < 6){
        d[i]++;
        break;
      }else{
        d[i]=0;
      }
    }
  }
  return 1;
}

int
is_valid(int* d, int n){
  int flag[7];
  for(int i=0; i<7; i++) flag[i] =0;

  for(int i = 0; i < n; i++){
    int x = d[i];
    if(flag[x]){
      return 0;
    }else{
      flag[x]= 1;
    }
  }

  return 1;
}

int
is_valid_2(int* d1, int* d2, int n1, int n2){

  for(int i = 0; i < n1; i++){
    for(int j = 0; j < n2; j++){
      if(d1[i]==d2[j]) return 0;
    }
  }

//  for(int i = 0; i < n1; i++){
//    printf("=%d ", d1[i]);
//  }
//  printf("\n");
//
//  for(int i = 0; i < n2; i++){
//    printf("=%d ", d2[i]);
//  }
//  printf("\n");

  return 1;
}

int
main(){
  int hour[N];
  int minute[N];

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

  int ndd=1;
  int seven = 7;
  int rslt=7;
  while(rslt<=n-1){
    rslt +=6*seven;
    seven *=7;
    ndd++;
  }

  int ndm=1;
  seven = 7;
  rslt=7;
  while(rslt<=m-1){
    rslt +=6*seven;
    seven *=7;
    ndm++;
  }

  //Debug
//  printf("--%d %d\n",ndd,ndm);
  //return 0;

  if(ndd+ndm>7){
    printf("%d\n",0);
    return 0;
  }

  //Now ndd and ndm are really small, I will use brute force.
  int count = 0;

  for(int i=0; i< ndd; i++) hour[i] = 0;

  //Debug
  //int d[] = {6,6,6,6,6};
  //int x = dec_num(d,5);
  //printf("?? %d\n",x);

//  do {
//    int x = dec_num(hour,ndd);
//      for(int i=0; i< ndm; i++) minute[i] = 0;
//      do {
//        int y = dec_num(minute,ndm);
//        printf(">>%d %d\n", x,y);
//      }while( next(minute,ndm, m) );
//  }while( next(hour,ndd,n) );

  do{
    if( is_valid(hour,ndd) ){
      for(int i=0; i< ndm; i++) minute[i] = 0;
      do{
        if( is_valid(minute,ndm) ){
          if( is_valid_2(hour,minute, ndd,ndm) ){
            count++;
//            int x, y;
//            x = dec_num(hour,ndd);
//            y = dec_num(minute,ndm);
//            printf("--%d %d\n", x,y);
          }
        }
      }while( next(minute,ndm, m) );
    }
  }while( next(hour,ndd,n) );

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

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

#define clr(ar) memset(ar, 0, sizeof(ar))
#define read() freopen("lol.txt", "r", stdin)

char visited[2][2][25][1025];
long long dp[2][2][25][1025];
int len[2], ar[2][25], temp[25];

void convert(int idx, int v){
    int i, j, k, d = 0;
    for (; ;){
        temp[d++] = (v % 7);
        v /= 7;
        if (!v) break;
    }

    len[idx] = 0;
    for (i = d - 1; i >= 0; i--) ar[idx][len[idx]++] = temp[i];
}

long long F(int idx, int flag, int i, int mask){
    if (i == len[idx]){
        if (idx == 0) return F(1, 0, 0, mask);
        else return 1;
    }
    if (visited[idx][flag][i][mask]) return dp[idx][flag][i][mask];

    int j, lim = 6;
    long long res = 0;
    if (!flag) lim = ar[idx][i];
    for (j = 0; j <= lim; j++){
        if (!(mask & (1 << j))) res += F(idx, flag || (j < lim), i + 1, mask | (1 << j));
    }

    visited[idx][flag][i][mask] = 1;
    return (dp[idx][flag][i][mask] = res);
}

int main(){
    int n, m, i, j, k;

    while (scanf("%d %d", &n, &m) != EOF){
        n--, m--;
        convert(0, n);
        convert(1, m);
        clr(visited);
        long long res = F(0, 0, 0, 0);
        printf("%lld\n", res);
    }
    return 0;
}

#include <stdio.h>

long long n, m, pow[11], nn = 0, mm = 0, res = 0, leng;
int a[7],x[8], res1 = 0, res2 = 0;

void kiemtra()
{
    int j, i = 1;
    long long h = 0;
    for (i = 1; i <= res1; i++)
    {
        h = h * 10 + x[i];
    }
    if (h >= nn) return;

    h = 0;
    for (i = res1 + 1; i <= leng; i++)
    {
        h = h * 10 + x[i];
    }
    if (h >= mm) return;
    res++;
}

void xuli(int i)
{
    int j;
    for (j = 0; j < 7; j++)
        if (a[j] == 0)
        {
            x[i] = j;
            a[j] = 1;
            if (i == leng) kiemtra();
            else
                xuli(i+1);
            a[j] = 0;
        }
}

void sol()
{
    int  x, i;
    for (i = 10; i >= 0; i--)
    {
        nn = nn * 10 + n / pow[i];
        if (n / pow[i] > 0)
        {
            x = n / pow[i];
            n -= x * pow[i];
            if (res1 == 0)
            {
                res1 = i+1;
            }
        }
    }
    for (i = 10; i >= 0; i--)
    {
        mm = mm * 10 + m / pow[i];
        if (m / pow[i] > 0)
        {
            x = m / pow[i];
            m -= x * pow[i];
            if (res2 == 0)
            {
                res2 = i+1;
            }
        }
    }
    leng = res1 + res2;
    if (res1 + res2 > 7) return;
    else
        xuli(1);
}

int main()
{
    int i, j;
    scanf("%I64d %I64d", &n, &m);
    for (j = 0; j < 7; j++)
        a[j] = 0;
    pow[0] = 1;
    for (i = 1; i < 11; i++)
        pow[i] = pow[i-1] * 7;
    sol();
    printf("%d", res);
    return 0;
}

#include <stdio.h>

long long n, m, pow[11], nn = 0, mm = 0, res = 0, leng;
int a[7],x[8], res1 = 0, res2 = 0;

void kiemtra()
{
    int j, i = 1;
    long long h = 0;
    for (i = 1; i <= res1; i++)
    {
        h = h * 10 + x[i];
    }
    if (h > nn) return;

    h = 0;
    for (i = res1 + 1; i <= leng; i++)
    {
        h = h * 10 + x[i];
    }
    if (h > mm) return;
    res++;
}

void xuli(int i)
{
    int j;
    for (j = 0; j < 7; j++)
        if (a[j] == 0)
        {
            x[i] = j;
            a[j] = 1;
            if (i == leng) kiemtra();
            else
                xuli(i+1);
            a[j] = 0;
        }
}

void sol()
{
    int  x, i;
    for (i = 10; i >= 0; i--)
    {
        nn = nn * 10 + n / pow[i];
        if (n / pow[i] > 0)
        {
            x = n / pow[i];
            n -= x * pow[i];
            if (res1 == 0)
            {
                res1 = i+1;
            }
        }
    }
    for (i = 10; i >= 0; i--)
    {
        mm = mm * 10 + m / pow[i];
        if (m / pow[i] > 0)
        {
            x = m / pow[i];
            m -= x * pow[i];
            if (res2 == 0)
            {
                res2 = i+1;
            }
        }
    }
    leng = res1 + res2;
    xuli(1);
}

int main()
{
    int i, j;
    scanf("%I64d %I64d", &n, &m);
    n--; m--;
    for (j = 0; j < 7; j++)
        a[j] = 0;
    pow[0] = 1;
    for (i = 1; i < 11; i++)
        pow[i] = pow[i-1] * 7;
    sol();
    printf("%d", res);
    return 0;
}

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define MAXN 13
long long a[MAXN],b[MAXN],m,n,ans=0;
long long s1[MAXN],s2[MAXN];
void find(long long x,long long y,long long add)
{
	long long i,j,S;
	if(add>y){
		ans++;
		return;
	}
	for(i=0;i<=6;i++){
		for(j=1;j<add;j++)
			if(i==b[j])
				goto end1;
		for(j=1;j<=x;j++)
			if(i==a[j])
				goto end1;
		s2[add]=i*pow(7,y-add);
		S=0;
		for(j=1;j<=add;j++)
			S+=s2[j];
		if(S>=m)
			goto end1;
		b[add]=i;
		find(x,y,add+1);
		end1:;
	}
}
void dfs(long long x,long long y,long long add)
{
	long long i,j,S;
	if(add>x){
		find(x,y,1);
		return;
	}
	for(i=0;i<=6;i++){
		for(j=1;j<add;j++){
			if(i==a[j])
			goto end;
		}
		s1[add]=i*pow(7,x-add);
		S=0;
		for(j=1;j<=add;j++)
			S+=s1[j];
		if(S>=n)
			goto end;
		a[add]=i;
		dfs(x,y,add+1);
		end:;
	}
}
int main()
{
	long long k,i,j,l,c,p,q,c1=0,c2=0;
	scanf("%I64d%I64d",&n,&m);
	p=n-1;q=m-1;
	while(p>0){
		p=p/7;
		c1++;
	}
	while(q>0){
		q=q/7;
		c2++;
	}
	if(c1+c2>7){
		printf("0");
		return 0;
	}
	dfs(c1,c2,1);
	printf("%I64d",ans);
	return 0;
}

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define MAXN 13
long long a[MAXN],b[MAXN],m,n,ans,count;
long long s1[MAXN],s2[MAXN];
void find(long long x,long long y,long long add)
{
	long long i,j,S;
	if(add>y){
		ans++;
		return;
	}
	for(i=0;i<=6;i++){
		for(j=1;j<add;j++)
			if(i==b[j])
				goto end1;
		for(j=1;j<=x;j++)
			if(i==a[j])
				goto end1;
		s2[add]=i*pow(7,y-add);
		S=0;
		for(j=1;j<=add;j++)
			S+=s2[j];
		if(S>=m)
			goto end1;
		b[add]=i;
		find(x,y,add+1);
		end1:;
	}
}
void dfs(long long x,long long y,long long add)
{
	long long i,j,S;
	if(add>x){
		find(x,y,1);
		return;
	}
	for(i=0;i<=6;i++){
		for(j=1;j<add;j++){
			if(i==a[j])
			goto end;
		}
		s1[add]=i*pow(7,x-add);
		S=0;
		for(j=1;j<=add;j++)
			S+=s1[j];
		if(S>=n)
			goto end;
		a[add]=i;
		dfs(x,y,add+1);
		end:;
	}
}
int main()
{
	long long k,i,j,l,c,p,q,c1=0,c2=0;
	scanf("%I64d%I64d",&n,&m);
	p=n;q=m;
	while(p>0){
		p=p/7;
		c1++;
	}
	while(q>0){
		q=q/7;
		c2++;
	}
	dfs(c1,c2,1);
	printf("%I64d",ans);
	return 0;
}

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define MAXN 11
long long a[MAXN],b[MAXN],m,n,ans,count;
long long s1[MAXN],s2[MAXN];
void find(long long x,long long y,long long add)
{
	long long i,j,S;
	if(add>y){
		count++;

		return;
	}
	for(i=0;i<=6;i++){
		for(j=1;j<add;j++)
			if(i==b[j])
				goto end1;
		for(j=1;j<=x;j++)
			if(i==a[j])
				goto end1;
		s2[add]=i*pow(7,y-add);
		S=0;
		for(j=1;j<=add;j++)
			S+=s2[j];
		if(S>=m)
			goto end1;
		b[add]=i;
		find(x,y,add+1);
		end1:;
	}
}
void dfs(long long x,long long y,long long add)
{
	long long i,j,S;
	if(add>x){
		count=0;
		find(x,y,1);
		ans+=count;
		return;
	}
	for(i=0;i<=6;i++){
		for(j=1;j<add;j++){
			if(i==a[j])
			goto end;
		}
		s1[add]=i*pow(7,x-add);
		S=0;
		for(j=1;j<=add;j++)
			S+=s1[j];
		if(S>=n)
			goto end;
		a[add]=i;
		dfs(x,y,add+1);
		end:;
	}
}

int main()
{
	long long k,i,j,l,c,p,q,c1=0,c2=0;
	scanf("%I64d%I64d",&n,&m);
	p=n;q=m;
	while(p>0){
		p=p/7;
		c1++;
	}
	while(q>0){
		q=q/7;
		c2++;
	}
	if(c1+c2>9){
		printf("0");
		return 0;
	}
	dfs(c1,c2,1);
	printf("%I64d",ans);
	return 0;
}

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define MAXN 13
long long a[MAXN],b[MAXN],m,n,ans=0;
long long s1[MAXN],s2[MAXN];
void find(long long x,long long y,long long add)
{
	long long i,j,S;
	if(add>y){
		ans++;
		return;
	}
	for(i=0;i<=6;i++){
		for(j=1;j<add;j++)
			if(i==b[j])
				goto end1;
		for(j=1;j<=x;j++)
			if(i==a[j])
				goto end1;
		s2[add]=i*pow(7,y-add);
		S=0;
		for(j=1;j<=add;j++)
			S+=s2[j];
		if(S>=m)
			goto end1;
		b[add]=i;
		find(x,y,add+1);
		end1:;
	}
}
void dfs(long long x,long long y,long long add)
{
	long long i,j,S;
	if(add>x){
		find(x,y,1);
		return;
	}
	for(i=0;i<=6;i++){
		for(j=1;j<add;j++){
			if(i==a[j])
			goto end;
		}
		s1[add]=i*pow(7,x-add);
		S=0;
		for(j=1;j<=add;j++)
			S+=s1[j];
		if(S>=n)
			goto end;
		a[add]=i;
		dfs(x,y,add+1);
		end:;
	}
}
int main()
{
	long long k,i,j,l,c,p,q,c1=0,c2=0;
	scanf("%I64d%I64d",&n,&m);
	p=n-1;q=m-1;
	while(p>0){
		p=p/7;
		c1++;
	}
	while(q>0){
		q=q/7;
		c2++;
	}
	if(c1<1)
	c2++;
	if(c2<1)
		c2++;
	if(c1+c2>7){
		printf("0");
		return 0;
	}
	dfs(c1,c2,1);
	printf("%I64d",ans);
	return 0;
}

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define MAXN 10
long long a[MAXN],b[MAXN],m,n,ans,count;
long long s1[MAXN],s2[MAXN];
void find(long long x,long long y,long long add)
{
	long long i,j,S;
	if(add>y){
		count++;

		return;
	}
	for(i=0;i<=6;i++){
		for(j=1;j<add;j++)
			if(i==b[j])
				goto end1;

		for(j=1;j<=x;j++)
			if(i==a[j])
				goto end1;
		s2[add]=i*pow(7,y-add);
		S=0;
		for(j=1;j<=add;j++)
			S+=s2[j];
		if(S>=m)
			goto end1;
		b[add]=i;
		find(x,y,add+1);
		end1:;
	}
}
void dfs(long long x,long long y,long long add)
{
	long long i,j,S;
	if(add>x){
		count=0;
		find(x,y,1);
		ans+=count;
		return;
	}
	for(i=0;i<=6;i++){
		for(j=1;j<add;j++){
			if(i==a[j])
			goto end;
		}
		s1[add]=i*pow(7,x-add);
		S=0;
		for(j=1;j<=add;j++)
			S+=s1[j];
		if(S>=n)
			goto end;
		a[add]=i;
		dfs(x,y,add+1);
		end:;
	}
}

int main()
{
	long long k,i,j,l,c,p,q,c1=0,c2=0;
	scanf("%I64d%I64d",&n,&m);
	p=n;q=m;
	while(p>0){
		p=p/7;
		c1++;
	}
	while(q>0){
		q=q/7;
		c2++;
	}
	if(c1+c2>7){
		printf("0");
		return 0;
	}
	dfs(c1,c2,1);
	printf("%I64d",ans);
	return 0;
}

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
int cmp[250];
int cnt[10];
int cmp1[250];
int calc(int a){
	int cntx=0;
	while(a){
		a/=7;
		cntx++;
	}
	return cntx;
}
int insert(int x,int fix[]){
	if(x==0){
		fix[0]=0;
		return 1;
	}
	int i,k; i=k=0;
	while(x){
		fix[i++]=x%7;
		x/=7;
	}
	for(k=0;k<i/2;k++){
		int temp = fix[k];
		fix[k] = fix[i-k-1];
		fix[i-k-1]=temp;
	}

	return i;
}
int fill(int x,int fix[],int t){
	int i,k; i=k=0; int lock[250];
	for(i=0;i<x;i++){
		lock[i]=fix[i];
	}
	for(i=0;i<t-x;i++){
		fix[i]=0;
	}
	for(i=t-x;i<x+(t-x);i++){
		fix[i] = lock[k++];
	}
}
int same(int x,int y){
	int i;
	memset(cnt,0,sizeof(cnt));
	for(i=0;i<x;i++){
		cnt[cmp[i]]++;
	}
	for(i=0;i<y;i++){
		cnt[cmp1[i]]++;
	}
	for(i=0;i<7;i++){
		if(cnt[i]>1)
		return 0;
	}
	return 1;
}
int main(){
	int a,b,c,d,e,f,g,h;
	scanf("%d%d",&a,&b);
	int t = calc(a-1); int t1=calc(b-1);
	if(t+t1>7){
		return 0;
	}
	int i; int cnt=0;
	for(i=0;i<a;i++){
		int val = insert(i,cmp);
		if(val<t){
			fill(val,cmp,t);
			val=val+(t-val);
		}
		for(c=0;c<b;c++){
			int val1 = insert(c,cmp1);
			if(val1<t1){
				fill(val1,cmp1,t1);
				val1=val1+(val-t1);
			}
			if(same(val,val1))
			cnt++;
		}
	}
	printf("%d",cnt);
}

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
int cmp[250];
int cnt[10];
int cmp1[250];
int calc(int a){
	int cntx=0;
	while(a){
		a/=7;
		cntx++;
	}
	return cntx;
}
int insert(int x,int fix[]){
	if(x==0){
		fix[0]=0;
		return 1;
	}
	int i,k; i=k=0;
	while(x){
		fix[i++]=x%7;
		x/=7;
	}
	for(k=0;k<i/2;k++){
		int temp = fix[k];
		fix[k] = fix[i-k-1];
		fix[i-k-1]=temp;
	}

	return i;
}
int fill(int x,int fix[],int t){
	int i,k; i=k=0; int lock[250];
	for(i=0;i<x;i++){
		lock[i]=fix[i];
	}
	for(i=0;i<t-x;i++){
		fix[i]=0;
	}
	for(i=t-x;i<x+(t-x);i++){
		fix[i] = lock[k++];
	}
}
int same(int x,int y){
	int i;
	memset(cnt,0,sizeof(cnt));
	for(i=0;i<x;i++){
		cnt[cmp[i]]++;
	}
	for(i=0;i<y;i++){
		cnt[cmp1[i]]++;
	}
	for(i=0;i<7;i++){
		if(cnt[i]>1)
		return 0;
	}
	return 1;
}
int main(){
	int a,b,c,d,e,f,g,h;
	scanf("%d%d",&a,&b);
	int t = calc(a-1); int t1=calc(b-1);
	if(t+t1>7){
		printf("0");
		return 0;
	}
	int i; int cnt=0;
	for(i=0;i<a;i++){
		int val = insert(i,cmp);
		if(val<t){
			fill(val,cmp,t);
			val=val+(t-val);
		}
		for(c=0;c<b;c++){
			int val1 = insert(c,cmp1);
			if(val1<t1){
				fill(val1,cmp1,t1);
				val1=val1+(val-t1);
			}
			if(same(val,val1))
			cnt++;
		}
	}
	printf("%d",cnt);
}

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
char cmp[250];
int cnt[10];
char cmp1[250];
void re_use(char temp[],int t){
	char new_arr[250];
	int len = strlen(temp); int i;
	for(i=0;i<t-len;i++){
		new_arr[i]='0';
	} int k=0;
	for(i=t-len;i<len+t;i++){
		new_arr[i] = temp[k++];
	}

	strcpy(temp,new_arr);
}
int calc(int a){
	int cntx=0;
	while(a){
		a/=7;
		cntx++;
	}
	return cntx;
}
int check(){
	int len = strlen(cmp); int len1=strlen(cmp1);
	int i;
	memset(cnt,0,sizeof(cnt));
	for(i=0;i<len;i++){
		cnt[cmp[i]-'0']++;
	}
	for(i=0;i<len1;i++){
		cnt[cmp1[i]-'0']++;
	}
	for(i=0;i<7;i++){
		if(cnt[i]>1)
			return 0;
	}
	return 1;
}
int main(){
	int a,b,c,d,e,f,g,h;
	scanf("%d%d",&a,&b);
	int t = calc(a); int t1=calc(b);
	if(t+t1>7){
		printf("0");
		return 0;
	}
	int i; int cnt=0;
	for(i=0;i<a;i++){
		itoa(i,cmp,7);
		if(strlen(cmp)<t){
			re_use(cmp,t);
		}
		for(c=0;c<b;c++){
			itoa(c,cmp1,7);
			if(strlen(cmp1)<t1){
				re_use(cmp1,t1);
			}
			if(check())
			cnt++;
		}
	}
	printf("%d",cnt);

}

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

#define SET(a,b) memset(a,b,sizeof(a))
#define scan(n) scanf("%d",&n)
#define print(n) printf("%d\n",n)
#define scanl(n) scanf("%lld",&n)
#define printl(n) printf("%lld\n",n)

//stacks
/*void push(int n,int ar[],int i)
  {
  ar[i]=n;
  i++;
  }
  void pop(int ar[],int i)
  {
  if(i>0)
  {
  ar[i-1]=0;
  i--;
  }
  }*/

//queues
/*void qpush(int ar[],int val,int head,int tail,int range)
  {
  if(head==tail)
  {
  ar[head]=val;
  if(tail+1==range)
  tail=0;
  else
  tail++;
  }
  else
  {
  ar[tail]=val;
  if(tail+1==range)
  tail=0;
  else
  tail++;
  }
  if(tail==head)
  {
  if(head+1==range)
  head=0;
  else
  head++;
  }
  }

  void qpop(int head,int tail,int range)
  {
  if(head+1==range)
  head=0;
  else
  head++;
  }*/

//pow
/*int fun(int a,int b,int m)
  {
  if(b==0)
  return 1;
  int d,x;
  d=fun(a,b/2,m);
  x=(d*d)%m;
  if(b%2==0)
  return x;
  else
  return (x*a%m)%m;
  }
  */
//bst
/*typedef struct tree
  {
  int val;
  struct tree *l;
  struct tree *r;
  }node;
//node *head;
node *insert(int x,node *root)
{
if(root==NULL)
{
root=(node *)malloc(sizeof(node));
root->val=x;
}
else
{
if(x<=root->val)
{
if(root->l==NULL)
{
root->l=(node *)malloc(sizeof(node));
root->l->val=x;
}
else
root->l=insert(x,root->l);
}
else
{
if(root->r==NULL)
{
root->r=(node *)malloc(sizeof(node));
root->r->val=x;
}
else
{
root->r=insert(x,root->r);
}
}
}
return root;
}
node *find(int x,node *root)
{
if(root==NULL)
{
return 0;
}
else
{
if(x==root->val)
return root;
else if(x<root->val)
{
if(root->l==NULL)
{
return 0;
}
else
{
find(x,root->l);
}
}
else
{
if(root->r==NULL)
{
return 0;
}
else
{
find(x,root->r);
}
}
}
}

void inorder(node *root)
{
       if(root==NULL)
       {
              return;
       }
       if(root->l==NULL)
       {
              printf("%d",root->val);
              if(root->r==NULL)
                     return;
              else
                     inorder(root->r);
       }
       else
       {
              inorder(root->l);
              printf("%d",root->val);
              if(root->r==NULL)
                     return;
              else
                     inorder(root->r);
       }
}
void preorder(node *root)
{
       if(root==NULL)
       {
              return;
       }
       else
       {
              printf("%d",root->val);
              preorder(root->l);
              preorder(root->r);
       }
}

void postorder(node *root)
{
       if(root==NULL)
       {
              return;
       }
       else
       {
              postorder(root->l);
              postorder(root->r);
              printf("%d",root->val);
       }
}
*/
//adjacency list
/*typedef struct ll
  {
  int val;
  struct ll *next;
  }nd;

//n *ar[100001];
void insert(int a,int b)
{
if(ar[a]==NULL)
{
ar[a]=(nd *)malloc(sizeof(nd));
ar[a]->val=b;
ar[a]->next=NULL;
}
else
{
nd *temp=(nd *)malloc(sizeof(nd)),*t;
temp->val=b;
temp->next=ar[a];
ar[a]=temp;
}
}

//dfs
//int state[100001];
void dfs(int a)
{
state[a]=1;
count++;
nd *temp=ar[a];
while(temp!=NULL)
{
if(state[temp->val]!=1)
dfs(temp->val);
temp=temp->next;
}
return;
}*/

//dijkstra + heaps
/*typedef struct ll2
  {
  int val;
  int weight;
  struct ll2 *next;
  }nde;
  */
//nde *ar[500005];
//int state[500005];

/*void insert(int a,int b,int w)//insert to adjacency list
  {
  if(ar[a]==NULL)
  {
  ar[a]=(node *)malloc(sizeof(node));
  ar[a]->val=b;
  ar[a]->weight=w;
  ar[a]->next=NULL;
  }
  else
  {
  node *temp=(node *)malloc(sizeof(node)),*t;
  temp->val=b;
  temp->weight=w;
  temp->next=ar[a];
  ar[a]=temp;
  }
  }*/

//int pt[500005],d[500005];
//int heap[500005][2];
//int ptr;
/*void decpty(int val,int pos)//decrease priority
  {
  heap[pos][0]=val;
  int x=pos;
  while(x>0)
  {
  if(heap[x][0]>=heap[x/2][0])
  break;
  else
  {
  pt[heap[x][1]]=x/2;
  pt[heap[x/2][1]]=x;
  int temp=heap[x][0],t2=heap[x][1];
  heap[x][0]=heap[x/2][0];
  heap[x][1]=heap[x/2][1];
  heap[x/2][0]=temp;
  heap[x/2][1]=t2;
  x=x/2;
  }
  }
  }

  void hinsert(int val,int val2)//insert to heap
  {
  heap[ptr][0]=val;
  heap[ptr][1]=val2;
  pt[val2]=ptr;
  int x=ptr;
  ptr++;
  while(x>0)
  {
  if(heap[x][0]>=heap[x/2][0])
  break;
  else
  {
  pt[heap[x][1]]=x/2;
  pt[heap[x/2][1]]=x;
  int temp=heap[x][0],t2=heap[x][1];
  heap[x][0]=heap[x/2][0];
  heap[x][1]=heap[x/2][1];
  heap[x/2][0]=temp;
  heap[x/2][1]=t2;
  x=x/2;
  }
  }
  }

  int hdeletemin()//delete min elem from heap
  {
  int ret;
  ret=heap[1][1];
  pt[heap[1][1]]=-1;
  pt[heap[ptr-1][1]]=1;
  heap[1][0]=heap[ptr-1][0];
  heap[1][1]=heap[ptr-1][1];
  ptr--;
  int x=1;
  while(x<ptr)
  {
  if(2*x+1>=ptr)
  {
  if(2*x<ptr)
  {
  if(heap[2*x][0]<heap[x][0])
  {
  pt[heap[x][1]]=2*x;
  pt[heap[2*x][1]]=x;
  int temp=heap[x][0];
  int temp2=heap[x][1];
  heap[x][0]=heap[2*x][0];
  heap[x][1]=heap[2*x][1];
heap[2*x][0]=temp;
heap[2*x][1]=temp2;
x=2*x;
}
else
break;
}
else
break;
}
else if(heap[x][0]<=heap[2*x][0] && heap[x][0]<=heap[2*x+1][0])
       break;
       else
{
       if(heap[2*x][0]<=heap[2*x+1][0])
       {
              pt[heap[x][1]]=2*x;
              pt[heap[2*x][1]]=x;
              int temp=heap[x][0];
              int temp2=heap[x][1];
              heap[x][0]=heap[2*x][0];
              heap[x][1]=heap[2*x][1];
              heap[2*x][0]=temp;
              heap[2*x][1]=temp2;
              x=2*x;
       }
       else
       {
              pt[heap[x][1]]=2*x+1;
              pt[heap[2*x+1][1]]=x;
              int temp=heap[x][0];
              int temp2=heap[x][1];
              heap[x][0]=heap[2*x+1][0];
              heap[x][1]=heap[2*x+1][1];
              heap[2*x+1][0]=temp;
              heap[2*x+1][1]=temp2;
              x=2*x+1;
       }
}
}
return ret;
}

void dikstra(int u,int v)
{
       while(ptr!=1)
       {
              int v=hdeletemin();
              node *temp=ar[v];
              while(temp!=NULL)
              {
                     int w=temp->val;
                     if(d[v]+temp->weight < d[w])
                     {
                            d[w]=d[v]+temp->weight;
                            decpty(d[w],pt[w]);
                     }
                     temp=temp->next;
              }
       }
}*/

/*int main()
  {
  int n,m,u,v,w,i;
  scanf("%d%d",&n,&m);
  for(i=0;i<=n;i++)
  state[i]=0;
  while(m--)
  {
  scanf("%d%d%d",&u,&v,&w);
  insert(u,v,w);
  }
  scanf("%d%d",&u,&v);
  ptr=1;
  hinsert(0,u);
  d[u]=0;
  for(i=1;i<=n;i++)
  {
  if(i!=u)
  {
  hinsert(1000000000,i);
  d[i]=1000000000;
  }
  }
  dikstra(u,v);
  if(d[v]==1000000000)
  printf("NO\n");
  else
  printf("%d\n",d[v]);
  return 0;
  }*/

//kruskal + dsu

//int dsu[N];
/*int find(int x)
  {
  if(dsu[x]==x)return x;
  return dsu[x] = find(dsu[x]);
  }

  void Union(int a,int b)
  {
  if(a==b)
  return;
  dsu[a] = b;
  }

  typedef struct edge
  {
  int u,v;
  int weight;
  }edge;
  */
//edge e[N];
/*int cmp(const void *a,const void *b)
  {
  return ((edge*)a)->weight - ((edge*)b)->weight;
  }*/
/*int main()
  {
  int i;
  int n,m;
  scanf("%d %d\n",&n,&m);
  for(i=0;i<m;i++)
  scanf("%d %d %d",&e[i].u,&e[i].v,&e[i].weight);
  for(i=1;i<=n;i++)
  dsu[i]=i;
  qsort(e,m,sizeof(edge),cmp);
  long long int sum=0;
  for(i=0;i<m;i++)
  {
  int a,b;
  if((a=find(e[i].u))!=(b=find(e[i].v)))
  {
  sum+=e[i].weight;
  Union(a,b);
  }
  }
  printf("%lld\n",sum);
  return 0;
  }*/

//max
/*int max(int a,int b)
  {
  if(a>b)
  return a;
  else
  return b;
  }
  */
//qsort

/*int cmpfunc(const void *a,const void *b)
  {
  return (*(int *)a - *(int *)b);
  }*/
//qsort(ar,n,sizeof(int),cmpfunc);

//-------------------------------------------------------------------------------------------------------------------------------------

int main()
{
       int n,m,ct,ans=0;
       scan(n);
       scan(m);
       int i,j,k,val=0;
       int dig1,dig2;
       val=n/7;
       dig1=1;
       dig2=1;
       while(val>0)
       {
              dig1++;
              val=val/7;
       }

       val=m/7;
       while(val>0)
       {
              dig2++;
              val=val/7;
       }
       val=dig1+dig2;
       if(val>7)
       {
              ans=0;
              print(ans);
              return 0;
       }
       int status[10],st2[10],cond=0;
       for(i=0;i<n;i++)
       {
              int ct1,ct2;
              ct1=0;
              int save=i;
              cond=0;
              for(j=0;j<10;j++)
                     status[j]=0;
              if(i==0)
              {
                     ct1++;
                     status[0]=1;
              }
              while(i>0)
              {
                     ct1++;
                     if(status[i%7]==1)
                     {
                            cond=1;
                            break;
                     }
                     else
                            status[i%7]=1;

                     i=i/7;
              }
              if(ct1<dig1)
              {
                     if(status[0]==1)
                            cond=1;
                     else
                            status[0]=1;
              }
              if(cond==1)
              {
                     i=save;
                     continue;
              }
              for(j=0;j<m;j++)
              {
                     ct2=0;
                     cond=0;
                     int k,save2=j;
        //                    printf("%d:%d\n",save,save2);
                     for(k=0;k<10;k++)
                            st2[k]=status[k];
                     if(j==0)
                     {
                            ct2++;
                            if(st2[j]==1)
                            {
                                   cond=1;
                                   j=save2;
                                   continue;
                            }
                            else
                                   st2[0]=1;
                     }
                     while(j>0)
                     {
                            ct2++;
                 //           printf("--%d\n",j%7);
                            if(st2[j%7]==1)
                            {
                                   cond=1;
                                   j=save2;
                                   break;
                            }
                            else
                                   st2[j%7]=1;

                            j=j/7;
                     }
                     if(ct2<dig2)
                     {
                            if(st2[0]==1)
                                   cond=1;
                            else
                                   st2[0]=1;
                     }
      //               for(k=0;k<10;k++)
    //                        printf("->%d ",st2[k]);
  //                   printf("\n");
//                     print(cond);
                     if(cond==0)
                     {
//                            printf("%d:%d\n",save,save2);
                            ans++;
                     }
                     j=save2;
              }
              i=save;
       }
       print(ans);
       return 0;
}

#include<stdio.h>
#include<math.h>
int dgt(int a)
{
    double r=log(a)/log(7);
    int c=r+1;
    return c;
}
int ys(int m, int n, int a, int b)
{
    int fr[7]={0,0,0,0,0,0,0},i;
    //printf("%d-%d: ",m,n);
    for(i=0;i<a;i++){
        fr[m%7]++;
        if(fr[m%7]>1){
            //printf("0\n");
            return 0;
        }
        m=m/7;
    }
    for(i=0;i<b;i++){
        fr[n%7]++;
        if(fr[n%7]>1){
                //printf("0\n");
            return 0;
        }
        n=n/7;
    }
    //printf("1\n");
    return 1;
}
int get(int m, int n)
{
    int d1,d2,i,j,rs=0;
    d1=dgt(m);
    d2=dgt(n);
    if(d1+d2>7)
        return 0;
    for(i=0;i<m;i++){
        if(ys(i,i,d1,0)){
            //printf("i=%d,d=%d\n",i,d1);
           for(j=0;j<n;j++){
                rs+=ys(i,j,d1,d2);
           }
        }
    }
    return rs;
}
int main()
{
    int m,n,result;
    scanf("%d %d",&m,&n);
    result=get(m,n);
    printf("%d\n",result);
    return 0;
}

#include<stdio.h>
#include<math.h>
int dgt(int a)
{
    double r=log(a)/log(7);
    int c=r;
    return (r==c)? c: c+1;
}
int ys(int m, int n, int a, int b)
{

    int fr[7]={0,0,0,0,0,0,0},i;
    for(i=0;i<a;i++){
        fr[m%7]++;
        if(fr[m%7]>1){

            return 0;
        }
        m=m/7;
    }
    for(i=0;i<b;i++){
        fr[n%7]++;
        if(fr[n%7]>1){

            return 0;
        }
        n=n/7;
    }

    return 1;
}
int get(int m, int n)
{
    int d1,d2,i,j,rs=0;
    d1=dgt(m);d2=dgt(n);
    if(d1+d2>7)
        return 0;
    for(i=0;i<m;i++){
        for(j=0;j<n;j++){
            rs+=ys(i,j,d1,d2);
        }
    }
    return rs;
}
int main()
{
    int m,n,result;
    scanf("%d %d",&m,&n);
    result=get(m,n);
    printf("%d\n",result);
    return 0;
}

#include<stdio.h>
#include<math.h>
int dgt(int a)
{
    double r;
    int c=1;
    if(a>1){ r=log(a-1)/log(7);
        c=r+1;
    }
    return c;
}
int ys(int m, int n, int a, int b)
{
    int fr[7]={0,0,0,0,0,0,0},i;
    //printf("%d-%d: ",m,n);
    for(i=0;i<a;i++){
        fr[m%7]++;
        if(fr[m%7]>1){
            //printf("0\n");
            return 0;
        }
        m=m/7;
    }
    for(i=0;i<b;i++){
        fr[n%7]++;
        if(fr[n%7]>1){
                //printf("0\n");
            return 0;
        }
        n=n/7;
    }
    //printf("1\n");
    return 1;
}
int get(int m, int n)
{
    int d1,d2,i,j,rs=0;
    d1=dgt(m);
    d2=dgt(n);
    //printf("D=%d,%d\n",d1,d2);
    if(d1+d2>7)
        return 0;
    for(i=0;i<m;i++){
        for(j=0;j<n;j++){
            rs+=ys(i,j,d1,d2);
        }
    }
    return rs;
}
int main()
{
    int m,n,result;
    scanf("%d %d",&m,&n);
    result=get(m,n);
    printf("%d\n",result);
    return 0;
}

#include<stdio.h>
#include<math.h>
int dgt(int a)
{
    double r=log(a)/log(7);
    int c=r+1;
    return c;
}
int ys(int m, int n, int a, int b)
{

    int fr[7]={0,0,0,0,0,0,0},i;
    for(i=0;i<a;i++){
        fr[m%7]++;
        if(fr[m%7]>1){

            return 0;
        }
        m=m/7;
    }
    for(i=0;i<b;i++){
        fr[n%7]++;
        if(fr[n%7]>1){

            return 0;
        }
        n=n/7;
    }

    return 1;
}
int get(int m, int n)
{
    int d1,d2,i,j,rs=0;
    d1=dgt(m);d2=dgt(n);
    if(d1+d2>7)
        return 0;
    for(i=0;i<m;i++){
        for(j=0;j<n;j++){
            rs+=ys(i,j,d1,d2);
        }
    }
    return rs;
}
int main()
{
    int m,n,result;
    scanf("%d %d",&m,&n);
    result=get(m,n);
    printf("%d\n",result);
    return 0;
}

#include<stdio.h>
#include<math.h>
int dgt(int a)
{
    double r;
    int c=1;
    if(a>1){ r=log(a-1)/log(7);
        c=r+1;
    }
    return c;
}
int ys(int m, int n, int a, int b)
{
    int fr[7]={0,0,0,0,0,0,0},i;
    //printf("%d-%d: ",m,n);
    for(i=0;i<a;i++){
        fr[m%7]++;
        if(fr[m%7]>1){
            //printf("0\n");
            return 0;
        }
        m=m/7;
    }
    for(i=0;i<b;i++){
        fr[n%7]++;
        if(fr[n%7]>1){
                //printf("0\n");
            return 0;
        }
        n=n/7;
    }
    //printf("1\n");
    return 1;
}
int get(int m, int n)
{
    int d1,d2,i,j,rs=0;
    d1=dgt(m);
    d2=dgt(n);
    if(d1+d2>7)
        return 0;
    for(i=0;i<m;i++){
        if(ys(i,i,d1,0)){
            //printf("i=%d,d=%d\n",i,d1);
           for(j=0;j<n;j++){
                rs+=ys(i,j,d1,d2);
           }
        }
    }
    return rs;
}
int main()
{
    int m,n,result;
    scanf("%d %d",&m,&n);
    result=get(m,n);
    printf("%d\n",result);
    return 0;
}

#include<stdio.h>
#include<math.h>
int dgt(int a)
{
    double r=log(a)/log(7);
    int c=r+1;
    return c;
}
int ys(int m, int n, int a, int b)
{
    int fr[7]={0,0,0,0,0,0,0},i;
    //printf("%d-%d: ",m,n);
    for(i=0;i<a;i++){
        fr[m%7]++;
        if(fr[m%7]>1){
        //    printf("0-1\n");
            return 0;
        }
        m=m/7;
    }
    for(i=0;i<b;i++){
        fr[n%7]++;
        if(fr[n%7]>1){
      //          printf("0-2\n");
            return 0;
        }
        n=n/7;
    }
    //printf("1\n");
    return 1;
}
int get(int m, int n)
{
    int d1,d2,i,j,rs=0;
    d1=dgt(m-1);
    if(d1<1)
        d1=1;
    d2=dgt(n-1);
    if(d2<1)
        d2=1;
    //printf("%d %d\n",d1,d2);
    if(d1+d2>7)
        return 0;
    for(i=0;i<m;i++){
        //if(ys(i,i,d1,0)){

            //printf("i=%d,d=%d\n",i,d1);
           for(j=0;j<n;j++){
                rs+=ys(i,j,d1,d2);
           //}
            }
    }
    return rs;
}
int main()
{
    int m,n,result;
    scanf("%d %d",&m,&n);
    result=get(m,n);
    printf("%d\n",result);
    return 0;
}

#include <stdio.h>

int n, m;
int dign, digm;
int res;

int dig(int k) {
    int ans=0;
    while(k>0) {
        k/=7;
        ans++;
    }
    return ans;
}

int chk(int h, int m) {
    int visit[7];
    for(int i=0;i<7;i++) visit[i]=0;
    for(int i=0;i<dign;i++) {
        visit[h%7]++;
        h/=7;
    }
    for(int i=0;i<digm;i++) {
        visit[m%7]++;
        m/=7;
    }
    for(int i=0;i<7;i++) {
        if(visit[i]>1) return 0;
    }
    return 1;
}

int main() {
    int tmp;
    scanf("%d %d", &n, &m);
    dign=dig(n), digm=dig(m);
    if(dign+digm>7) puts("0");
    else {
        for(int i=0;i<n;i++) {
            for(int j=0;j<m;j++) {
                if(chk(i, j)) res++;
            }
        }
        printf("%d", res);
    }
    return 0;
}

#include <stdio.h>

int n, m;
int dign, digm;
int res;

int dig(int k) {
    int ans=0;
    while(k>0) {
        k/=7;
        ans++;
    }
    return ans;
}

int chk(int h, int m) {
    int visit[7];
    for(int i=0;i<7;i++) visit[i]=0;
    for(int i=0;i<dign;i++) {
        visit[h%7]++;
        h/=7;
    }
    for(int i=0;i<digm;i++) {
        visit[m%7]++;
        m/=7;
    }
    for(int i=0;i<7;i++) {
        if(visit[i]>1) return 0;
    }
    return 1;
}

int main() {
    int tmp;
    scanf("%d %d", &n, &m);
    dign=dig(n-1), digm=dig(m-1);
    if(dign+digm>7) puts("0");
    else {
        for(int i=0;i<n;i++) {
            for(int j=0;j<m;j++) {
                if(chk(i, j)==1) res++;
            }
        }
        printf("%d", res);
    }
    return 0;
}

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

int main() {
	int n, m, d;
	scanf("%d%d", &n, &m);
	int dn = 1, dm = 1;
	for(int k = 7 ; k <= n ; k *= 7) dn++;
	for(int k = 7 ; k <= m ; k *= 7) dm++;
	d = dn + dm;
	if(d > 7) {
		printf("0\n");
		return 0;
	}
	int r = 0;
	for(int i=0 ; i<n ; i++) {
		for(int j=0 ; j<m ; j++) {
			int u[7] = {0};
			int a = i, b = j;
			for(int k=0 ; k<dn ; k++) {
				u[a%7]++;
				a /= 7;
			}
			for(int k=0 ; k<dm ; k++) {
				u[b%7]++;
				b /= 7;
			}
			r++;
			for(int k=0 ; k<7 ; k++) {
				if(u[k] > 1) {
					r--;
					break;
				}
			}
		}
	}
	printf("%d\n", r);
	return 0;
}

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

int arr[7];

int fn(int i,int j,int p,int q){
	int k,l;
	for(k=0;k<p;k++){
		arr[k]=i%7;
		i/=7;
	}
	for(k=p;k<p+q;k++){
		arr[k]=j%7;
		j/=7;
	}
	for(k=0;k<p+q;k++){
		for(l=k+1;l<p+q;l++){
			if(arr[l]==arr[k])return 0;
		}
	}
	return 1;
}

int fn1(int p,int q,int n,int m){
	int count=0,i,j;
	for(i=0;i<n;i++){
		for(j=0;j<m;j++){
			if(fn(i,j,p,q))count++;
		}
	}
	return count;
}

int main(){
	int n,nn,m,mm,p,q;
	scanf("%d%d",&n,&m);
	nn=n;
	mm=m;
	p=q=1;
	while(nn>6){
		p++;
		nn/=7;
	}
	while(mm>6){
		q++;
		mm/=7;
	}
	if(p+q>7)printf("0\n");
	else printf("%d\n",fn1(p,q,n,m));
	return 0;
}

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

int fn(int i,int j,int p,int q){
	int k,l,arr[8];
	for(k=0;k<p;k++){
		arr[k]=i%7;
		i/=7;
	}
	for(k=p;k<p+q;k++){
		arr[k]=j%7;
		j/=7;
	}
	for(k=0;k<p+q;k++){
		for(l=k+1;l<p+q;l++){
			if(arr[l]==arr[k])return 0;
		}
	}
	return 1;
}

int fn1(int p,int q,int n,int m){
	int count=0,i,j;
	for(i=0;i<n;i++){
		for(j=0;j<m;j++){
			if(fn(i,j,p,q))count++;
		}
	}
	return count;
}

int main(){
	int n,nn,m,mm,p,q;
	scanf("%d%d",&n,&m);
	nn=n;
	mm=m;
	p=q=1;
	while(nn>6){
		p++;
		nn/=7;
	}
	while(mm>6){
		q++;
		mm/=7;
	}
	if(p+q>7)printf("0\n");
	else printf("%d\n",fn1(p,q,n,m));
	return 0;
}

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

int fn(int i,int j,int p,int q,int n,int m){
	int k,l,arr[7];
	for(k=0;k<p;k++){
		arr[k]=i%7;
		i/=7;
	}
	for(k=p;k<p+q;k++){
		arr[k]=j%7;
		j/=7;
	}
	for(k=0;k<p+q;k++){
		for(l=k+1;l<p+q;l++){
			if(arr[l]==arr[k])return 0;
		}
	}
	return 1;
}

int fn1(int p,int q,int n,int m){
	int count=0,i,j;
	for(i=0;i<n;i++){
		for(j=0;j<m;j++){
			if(fn(i,j,p,q,n,m))count++;
		}
	}
	return count;
}

int main(){
	int n,m,p,q;
	scanf("%d%d",&n,&m);
	if(n==0)p=1;
	else p=((double)(log(n)/log(7.0)))+1;
	if(m==0)q=1;
	else q=((double)(log(m)/log(7.0)))+1;
	if(p+q>7)printf("0\n");
	else printf("%d\n",fn1(p,q,n,m));
	return 0;
}

#include <stdio.h>
#include <string.h>
#define NMAX 20
char hh[NMAX];
char mm[NMAX];
int xh[9];
short int fl[8];
int res;
int h, m;
int findans(int x, int ho, int mo)
{
	int i, th, tm;
	if (x > ho + mo)
	{
		th = 0;
		for (i = 0; i < ho; i++)
		{
			th = th * 7 + xh[i];
		}
		tm = 0;
		for (i = ho; i < ho + mo; i++)
		{
			tm = tm * 7 + xh[i];
		}
		if (th < h&&tm < m) res += 1;
	}
	else
	{
		for (i = 0; i < 7; i++)
		{
			if (fl[i] == 0)
			{
				xh[x - 1] = i;
				fl[i] = 1;
				findans(x + 1, ho, mo);
				fl[i] = 0;
			}
		}
	}
	return 0;
}
int main()
{
	int i, j, th, tm, d;
	int mo, ho;
	scanf("%d%d", &h, &m);
	th = h - 1;
	tm = m - 1;
	mo = ho = 0;
	while (tm > 0)
	{
		mm[mo] = tm % 7;
		tm = tm / 7;
		mo++;
	}
	while (th > 0)
	{
		hh[ho] = th % 7;
		ho++;
		th = th / 7;
	}
	if (ho + mo > 7) printf("0\n");
	else
	{
		memset(fl, 0, sizeof(fl));
		res = 0;
		findans(1, ho, mo);
		printf("%d\n", res);
	}
	return 0;
}

#include <stdio.h>
#include <string.h>
#define NMAX 20
char hh[NMAX];
char mm[NMAX];
int xh[9];
short int fl[8];
int res;
int h, m;
int findans(int x, int ho, int mo)
{
	int i, th, tm;
	if (x > ho + mo)
	{
		th = 0;
		for (i = 0; i < ho; i++)
		{
			th = th * 7 + xh[i];
		}
		tm = 0;
		for (i = ho; i < ho + mo; i++)
		{
			tm = tm * 7 + xh[i];
		}
		if (th < h&&tm < m) res += 1;
	}
	else
	{
		for (i=0;i<7;i++)
		{
			if (fl[i] == 0)
			{
				xh[x - 1] = i;
				fl[i] = 1;
				findans(x + 1, ho, mo);
				fl[i] = 0;
			}
		}
	}
	return 0;
}
int main()
{
	int i, j, th, tm, d;
	int mo, ho;
	scanf("%d%d", &h, &m);
	th = h - 1;
	tm = m - 1;
	mo = ho = 0;
	while (tm > 0)
	{
		mm[mo] = tm % 7;
		tm = tm / 7;
		mo++;
	}
	while (th > 0)
	{
		hh[ho] = th % 7;
		ho++;
		th = th / 7;
	}
	if (ho + mo > 8) printf("0\n");
	else
	{
		memset(fl, 0, sizeof(fl));
		res = 0;
		findans(1, ho, mo);
		printf("%d\n", res);
	}
	return 0;
}

#include <stdio.h>

int count(int n) {
	return n < 7 ? 1 : count(n / 7) + 1;
}

int check(int *ii, int ncnt, int mcnt, int n, int m) {
	int i, a, b;

	a = 0;
	for (i = 0; i < ncnt; i++)
		a = 7 * a + ii[i];
	b = 0;
	for (i = 0; i < mcnt; i++)
		b = 7 * b + ii[i + ncnt];
	return a < n && b < m;
}

int main() {
	int n, m, cnt, ncnt, mcnt, dcnt;
	static int ii[7], used[7];

	scanf("%d%d", &n, &m);
	ncnt = count(n);
	mcnt = count(m);
	dcnt = ncnt + mcnt;
	cnt = 0;
	for (ii[0] = 0; ii[0] < 7; ii[0]++) {
		if (used[ii[0]])
			continue;
		used[ii[0]] = 1;
		if (dcnt == 1 && check(ii, ncnt, mcnt, n, m))
			cnt++;
		for (ii[1] = 0; ii[1] < 7; ii[1]++) {
			if (used[ii[1]])
				continue;
			used[ii[1]] = 1;
			if (dcnt == 2 && check(ii, ncnt, mcnt, n, m))
				cnt++;
			for (ii[2] = 0; ii[2] < 7; ii[2]++) {
				if (used[ii[2]])
					continue;
				used[ii[2]] = 1;
				if (dcnt == 3 && check(ii, ncnt, mcnt, n, m))
					cnt++;
				for (ii[3] = 0; ii[3] < 7; ii[3]++) {
					if (used[ii[3]])
						continue;
					used[ii[3]] = 1;
					if (dcnt == 4 && check(ii, ncnt, mcnt, n, m))
						cnt++;
					for (ii[4] = 0; ii[4] < 7; ii[4]++) {
						if (used[ii[4]])
							continue;
						used[ii[4]] = 1;
						if (dcnt == 5 && check(ii, ncnt, mcnt, n, m))
							cnt++;
						for (ii[5] = 0; ii[5] < 7; ii[5]++) {
							if (used[ii[5]])
								continue;
							used[ii[5]] = 1;
							if (dcnt == 6 && check(ii, ncnt, mcnt, n, m))
								cnt++;
							for (ii[6] = 0; ii[6] < 7; ii[6]++) {
								if (used[ii[6]])
									continue;
								used[ii[6]] = 1;
								used[ii[6]] = 0;
								if (dcnt == 7 && check(ii, ncnt, mcnt, n, m))
									cnt++;
							}
							used[ii[5]] = 0;
						}
						used[ii[4]] = 0;
					}
					used[ii[3]] = 0;
				}
				used[ii[2]] = 0;
			}
			used[ii[1]] = 0;
		}
		used[ii[0]] = 0;
	}
	printf("%d\n", cnt);
	return 0;
}

#include <stdio.h>

int count(int n) {
	return n < 7 ? 1 : count(n / 7) + 1;
}

int check(int *ii, int ncnt, int mcnt, int n, int m) {
	int i;
	long long a, b;

	a = 0;
	for (i = 0; i < ncnt; i++)
		a = 7LL * a + ii[i];
	b = 0;
	for (i = 0; i < mcnt; i++)
		b = 7LL * b + ii[i + ncnt];
	return a < n && b < m;
}

int main() {
	int n, m, cnt, ncnt, mcnt, dcnt;
	static int ii[7], used[7];

	scanf("%d%d", &n, &m);
	ncnt = count(n);
	mcnt = count(m);
	dcnt = ncnt + mcnt;
	cnt = 0;
	for (ii[0] = 0; ii[0] < 7; ii[0]++) {
		if (used[ii[0]])
			continue;
		used[ii[0]] = 1;
		if (dcnt == 1 && check(ii, ncnt, mcnt, n, m))
			cnt++;
		for (ii[1] = 0; ii[1] < 7; ii[1]++) {
			if (used[ii[1]])
				continue;
			used[ii[1]] = 1;
			if (dcnt == 2 && check(ii, ncnt, mcnt, n, m))
				cnt++;
			for (ii[2] = 0; ii[2] < 7; ii[2]++) {
				if (used[ii[2]])
					continue;
				used[ii[2]] = 1;
				if (dcnt == 3 && check(ii, ncnt, mcnt, n, m))
					cnt++;
				for (ii[3] = 0; ii[3] < 7; ii[3]++) {
					if (used[ii[3]])
						continue;
					used[ii[3]] = 1;
					if (dcnt == 4 && check(ii, ncnt, mcnt, n, m))
						cnt++;
					for (ii[4] = 0; ii[4] < 7; ii[4]++) {
						if (used[ii[4]])
							continue;
						used[ii[4]] = 1;
						if (dcnt == 5 && check(ii, ncnt, mcnt, n, m))
							cnt++;
						for (ii[5] = 0; ii[5] < 7; ii[5]++) {
							if (used[ii[5]])
								continue;
							used[ii[5]] = 1;
							if (dcnt == 6 && check(ii, ncnt, mcnt, n, m))
								cnt++;
							for (ii[6] = 0; ii[6] < 7; ii[6]++) {
								if (used[ii[6]])
									continue;
								used[ii[6]] = 1;
								if (dcnt == 7 && check(ii, ncnt, mcnt, n, m))
									cnt++;
								used[ii[6]] = 0;
							}
							used[ii[5]] = 0;
						}
						used[ii[4]] = 0;
					}
					used[ii[3]] = 0;
				}
				used[ii[2]] = 0;
			}
			used[ii[1]] = 0;
		}
		used[ii[0]] = 0;
	}
	printf("%d\n", cnt);
	return 0;
}

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

const int N = 24;

int
dec_num(int* d, int n){
  int rslt = d[0];
  int seven_plus  = 7;
  for(int i = 1; i < n; i++){
    rslt += d[i] * seven_plus;
    seven_plus *=7;
  }
  return rslt;
}

int
next(int* d, int n, int h_limit){
  if( dec_num(d,n) >= h_limit - 1 ){
    return 0;
  }else{
    for(int i= 0; i< n; i++){
      if(d[i] < 6){
        d[i]++;
        break;
      }else{
        d[i]=0;
      }
    }
  }
  return 1;
}

int
is_valid(int* d, int n){
  int flag[7];
  for(int i=0; i<7; i++) flag[i] =0;

  for(int i = 0; i < n; i++){
    int x = d[i];
    if(flag[x]){
      return 0;
    }else{
      flag[x]= 1;
    }
  }

  return 1;
}

int
is_valid_2(int* d1, int* d2, int n1, int n2){

  for(int i = 0; i < n1; i++){
    for(int j = 0; j < n2; j++){
      if(d1[i]==d2[j]) return 0;
    }
  }

//  for(int i = 0; i < n1; i++){
//    printf("=%d ", d1[i]);
//  }
//  printf("\n");
//
//  for(int i = 0; i < n2; i++){
//    printf("=%d ", d2[i]);
//  }
//  printf("\n");

  return 1;
}

int
main(){
  int hour[N];
  int minute[N];

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

  int ndd = ceil( log(n) / log(6.) ); //Number of digits for day.
  int ndm = ceil( log(m) / log(6.) ); //Number of digits for minutes.

  //Debug
  //printf("--%d %d\n",ndd,ndm);

  if(ndd+ndm>=7){
    printf("%d\n",0);
    return 0;
  }

  //Now ndd and ndm are really small, I will use brute force.
  int count = 0;

  for(int i=0; i< ndd; i++) hour[i] = 0;

  //Debug
//  int d[] = {0,0,0};
//  int x = dec_num(hour,ndd);
//  printf("?? %d\n",x);

//  do {
//    int x = dec_num(hour,ndd);
//      for(int i=0; i< ndm; i++) minute[i] = 0;
//      do {
//        int y = dec_num(minute,ndm);
//        printf(">>%d %d\n", x,y);
//      }while( next(minute,ndm, m) );
//  }while( next(hour,ndd,n) );

  do{
    if( is_valid(hour,ndd) ){
      for(int i=0; i< ndm; i++) minute[i] = 0;
      do{
        if( is_valid(minute,ndm) ){
          if( is_valid_2(hour,minute, ndd,ndm) ){
            count++;
//            int x, y;
//            x = dec_num(hour,ndd);
//            y = dec_num(minute,ndm);
//            printf("--%d %d\n", x,y);
          }
        }
      }while( next(minute,ndm, m) );
    }
  }while( next(hour,ndd,n) );

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

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

const int N = 24;

int
dec_num(int* d, int n){
  int rslt = d[0];
  int seven_plus  = 7;
  for(int i = 1; i < n; i++){
    rslt += d[i] * seven_plus;
    seven_plus *=7;
  }
  return rslt;
}

int
next(int* d, int n, int h_limit){
  if( dec_num(d,n) >= h_limit - 1 ){
    return 0;
  }else{
    for(int i= 0; i< n; i++){
      if(d[i] < 6){
        d[i]++;
        break;
      }else{
        d[i]=0;
      }
    }
  }
  return 1;
}

int
is_valid(int* d, int n){
  int flag[7];
  for(int i=0; i<7; i++) flag[i] =0;

  for(int i = 0; i < n; i++){
    int x = d[i];
    if(flag[x]){
      return 0;
    }else{
      flag[x]= 1;
    }
  }

  return 1;
}

int
is_valid_2(int* d1, int* d2, int n1, int n2){

  for(int i = 0; i < n1; i++){
    for(int j = 0; j < n2; j++){
      if(d1[i]==d2[j]) return 0;
    }
  }

//  for(int i = 0; i < n1; i++){
//    printf("=%d ", d1[i]);
//  }
//  printf("\n");
//
//  for(int i = 0; i < n2; i++){
//    printf("=%d ", d2[i]);
//  }
//  printf("\n");

  return 1;
}

int
main(){
  int hour[N];
  int minute[N];

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

  int ndd = ceil( log(n) / log(6.) ); //Number of digits for day.
  int ndm = ceil( log(m) / log(6.) ); //Number of digits for minutes.

  if(ndd==0) ndd++;
  if(ndm==0) ndm++;

  //Debug
  //printf("--%d %d\n",ndd,ndm);

  if(ndd+ndm>=7){
    printf("%d\n",0);
    return 0;
  }

  //Now ndd and ndm are really small, I will use brute force.
  int count = 0;

  for(int i=0; i< ndd; i++) hour[i] = 0;

  //Debug
//  int d[] = {0,0,0};
//  int x = dec_num(hour,ndd);
//  printf("?? %d\n",x);

//  do {
//    int x = dec_num(hour,ndd);
//      for(int i=0; i< ndm; i++) minute[i] = 0;
//      do {
//        int y = dec_num(minute,ndm);
//        printf(">>%d %d\n", x,y);
//      }while( next(minute,ndm, m) );
//  }while( next(hour,ndd,n) );

  do{
    if( is_valid(hour,ndd) ){
      for(int i=0; i< ndm; i++) minute[i] = 0;
      do{
        if( is_valid(minute,ndm) ){
          if( is_valid_2(hour,minute, ndd,ndm) ){
            count++;
//            int x, y;
//            x = dec_num(hour,ndd);
//            y = dec_num(minute,ndm);
//            printf("--%d %d\n", x,y);
          }
        }
      }while( next(minute,ndm, m) );
    }
  }while( next(hour,ndd,n) );

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