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.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

	
	
public class Solution  {
	
	static class FastScanner{
		BufferedReader s;
		StringTokenizer st;
		
		
		public FastScanner(){
			st = new StringTokenizer("");
			s = new BufferedReader(new InputStreamReader(System.in));
		}
		
		public FastScanner(File f) throws FileNotFoundException{
			st = new StringTokenizer("");
			s = new BufferedReader (new FileReader(f));
		}
		
		public int nextInt() throws IOException{
			if(st.hasMoreTokens())
				return Integer.parseInt(st.nextToken());
			
			else{
				st = new StringTokenizer(s.readLine());
				return nextInt();
			}
		}
		
		public double nextDouble() throws IOException{
			if(st.hasMoreTokens())
				return Double.parseDouble(st.nextToken());
			else{
				st = new StringTokenizer(s.readLine());
				return nextDouble();
			}
		}
		
		public long nextLong() throws IOException{
			if(st.hasMoreTokens())
				return Long.parseLong(st.nextToken());
			else{
				st = new StringTokenizer(s.readLine());
				return nextLong();
			}
		}
		
		public String nextString() throws IOException{
			if(st.hasMoreTokens())
				return st.nextToken();
			else{
				st = new StringTokenizer(s.readLine());
				return nextString();
			}
			
		}
		public String readLine() throws IOException{
			return s.readLine();
		}
		
		public void close() throws IOException{
			s.close();
		}
		
	}
	
	
	
	
	//FastScanner s = new FastScanner(new File("input.txt"));       copy inside void solve
    //PrintWriter ww = new PrintWriter(new FileWriter("output.txt"));
    static FastScanner s = new FastScanner();  
    static PrintWriter ww = new PrintWriter(new OutputStreamWriter(System.out));

    public static void main(String args[])throws IOException{
    	//Main ob=new Main();
    	Solution ob=new Solution();
		ob.solve();
		ww.close();
    }
    //////////////////////////////////////////////
   
    
    //////////////////////////////////////////////
    
   int numofdigit(int n){
	   
	   int ans = 0;
	   
	   if(n==0)
		   return 1;
	   
	   while(n!=0){
		   n/=7;
		   ans+=1;
	   }
		   
	   return ans;
   }
    //////////////////////////////////////////////
    		 void solve() throws IOException {
    			
    			 int n = s.nextInt()-1;
    			 int m = s.nextInt()-1;
    			 
    			 int size1 = numofdigit(n);
    			 int size2 = numofdigit(m);
    			 
    			 int ans = 0;
    			 //System.out.println(size2+" "+size1);
    			 if(size1+size2<=7){
    			//System.out.println(size1+"  ");
    			 for(int i=0;i<=n;i++){
    				 for(int j=0;j<=m;j++){
    					
    					 if(i==j)
    						 continue;
    					 
    					 int arr[] = new int[7];
    					 int temp = size1;
    					 int num1 =i;
    				//	 System.out.print(num1+" ");
    					 while(num1!=0){
    						 arr[num1%7]+=1;
    						 num1/=7;
    						 temp--;
    					 }
    					 
    					 arr[0]+= temp;
    					 temp = size2;
    					 int num2 = j;
    				//	 System.out.println(num2);
    					 while(num2!=0){
    						 arr[num2%7]+=1;
    						 num2/=7;
    						 temp--;
    					 }
    					 
    					 arr[0]+=temp;
    					 
 //   					 System.out.println("dsf");
    					 boolean flag = false;
    					 for(int c=0;c<7;c++){
    						 if(arr[c]>1){
    							 flag = true;
    							 break;
    						 }
    					 }
    					 
    					 if(!flag)
    						 ans+=1;
    					 
    			//		 System.out.println(flag);
    					 
    				 }
    			 }
    			 }
    			 System.out.println(ans);
    			 
    		 }//solve

}	









