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.

Input

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.

Output

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.

Examples

Input

2 3


Output

4


Input

8 2


Output

5

Note

In 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).
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {

	static long memo[][][];
	
	static int[] h , m;
	static int hLen , mLen;
	static boolean hIsPow , mIsPow;
	
	static long dp(int idx,int mask , int lower)
	{
		if(idx<0)
			return lower==1 && mask!=0? 1:0;
		if(idx==mLen-1)
		{
			if(lower==0)return 0;
			lower = 0;
			if(mIsPow)lower = 1;
		}
		if(memo[idx][mask][lower]!=-1)return memo[idx][mask][lower];
		long ans = 0;
		int upper = 7;
		if(lower==0)
		{
			int curDigit = idx >= mLen? h[idx-mLen] : m[idx];
			if( (mask & (1<<curDigit)) == 0 )
				ans += dp(idx-1,mask|(1<<curDigit),0);
			upper = curDigit;
		}
		for (int i = 0; i < upper; i++) 
			if( (mask & (1<<i)) == 0 )
				ans += dp(idx-1,mask|(1<<i),1);
		
		return memo[idx][mask][lower] = ans;
	}
	
	static int decToSev(int n,int[] arr)
	{
		int idx = 0;
		while(n>0)
		{
			arr[idx++] = n%7;
			n/=7;
		}
		return idx;
	}
	
	public static void main(String[] args) throws Throwable {
		Scanner sc = new Scanner(System.in);
//		Scanner sc = new Scanner("out");
		int n = sc.nextInt() , k = sc.nextInt();
		h = new int[20]; m = new int[20];
		hLen = decToSev(n, h);
		mLen = decToSev(k, m);
		memo = new long[hLen+mLen+1][1<<7][2];
		for (int i = 0; i < memo.length; i++) 
			for (int j = 0; j < memo[i].length; j++) 
				Arrays.fill(memo[i][j], -1);
		int c1 = 0 , c2 = 0;
		for(int i : h)
		{
			if(i==1)++c1;
			else
			if(i!=0)++c2;
		}
		if(c2==0 && c1==1 && n!=1)
			hIsPow = true;
		c1 = c2 = 0; 
		for(int i : m)
		{
			if(i==1)++c1;
			else
				if(i!=0)++c2;
		}
		if(c2==0 && c1==1 && k!=1)
			mIsPow = true;
		if(hIsPow)--hLen;
		if(mIsPow)--mLen;
		if(hIsPow)
			System.out.println(dp(hLen+mLen-1,0,1));
		else
			System.out.println(dp(hLen+mLen-1,0,0));
	}
	
	static class Scanner 
	{
		StringTokenizer st;
		BufferedReader br;
		public Scanner(InputStream s){	br = new BufferedReader(new InputStreamReader(s));}
		public Scanner(String file) throws FileNotFoundException { br = new BufferedReader(new FileReader(file));}
		public String next() throws IOException 
		{
			while (st == null || !st.hasMoreTokens())st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}
		public int nextInt() throws IOException {return Integer.parseInt(next());}
		public long nextLong() throws IOException {return Long.parseLong(next());}
		public String nextLine() throws IOException {return br.readLine();}
		public double nextDouble() throws IOException
		{
			String x = next();
			StringBuilder sb = new StringBuilder("0");
			double res = 0, f = 1;
			boolean dec = false, neg = false;
			int start = 0;
			if(x.charAt(0) == '-'){neg = true;start++;}
			for(int i = start; i < x.length(); i++)
				if(x.charAt(i) == '.')
				{
					res = Long.parseLong(sb.toString());
					sb = new StringBuilder("0");
					dec = true;
				}
				else{sb.append(x.charAt(i));if(dec)f *= 10;}
			res += Long.parseLong(sb.toString()) / f;
			return res * (neg?-1:1);
		}
		public int[] nexIntArray() throws Throwable
		{
			st = new StringTokenizer(br.readLine());
			int[] a = new int[st.countTokens()];
			for(int i = 0; i < a.length;i++)a[i]=nextInt();
			return a;
		}
		public boolean ready() throws IOException {return br.ready();}
	}
}
