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.util.*;
	import java.io.*;
	import java.math.*;
	public class Main {
		public static long mod= (long) (1e9 +7);
		public static void main(String args[])
		{
			InputReader s= new InputReader(System.in);
			OutputStream outputStream= System.out;
			PrintWriter out= new PrintWriter(outputStream);
			int n= s.nextInt();
			int m= s.nextInt();
			int cnt=0;
		//	cnt += (n-1)%7==0?(n-1)/7+1:(n-1)/7;
		//	cnt += (m-1)%7==0?(m-1)/7+1:(m-1)/7;
			String st1 = Integer.toString(n-1,7);
			String st2 = Integer.toString(m-1,7);
			if(st1.length()+st2.length()>7)
				out.println(0);
			else{
//				String st1 = Integer.toString(n-1,7);
//				String st2 = Integer.toString(m-1,7);
//			//	out.println("dsa"+st1.length());
			//	out.println(st1+" "+st2);
				long sum=0;
				for(int i=n-1;i>=0;i--){
					String st3= Integer.toString(i,7);
					//out.println("length= "+st1.length()+" "+st3.length());
					int x= st3.length();
					for(int k=0;k<(st1.length()-x);k++){
					//	out.print("sds");
						st3+= '0';
					}
					for(int j=m-1;j>=0;j--){		
						String st4 = Integer.toString(j,7);
						int y= st4.length();
						for(int k=0;k<(st2.length()-y);k++){
							st4+='0';
						}
					//	out.println(st3+" "+st4);
						ArrayList<Character> a= new ArrayList<>();
					//	out.println(st3+" "+st4);
						int flag=0;
						for(int p=0;p<st3.length() && flag==0;p++){
							if(a.contains(st3.charAt(p)))
								flag=1;
							else
								a.add(st3.charAt(p));
						}
						for(int p=0;p<st4.length() && flag==0;p++){
							if(a.contains(st4.charAt(p)))
								flag=1;
							else
								a.add(st4.charAt(p));
						}
						if(flag==0){
					//		out.println(st3+" "+st4);
							sum++;
						}
						else
							flag=0;
					}
				}
				out.println(sum);
			}
			out.close();
		}
		
		static long combinations(long n,long r){   // O(r)
			if(r==0 || r==n) return 1;
			r= Math.min(r, n-r);
			long ans=n;  // nC1=n;
			for(int i=1;i<r;i++){
				ans= ans*(n-i);
				ans= ans/(i+1);
			}
			return ans;
			
		}
		public static void debug(Object... o) {
			System.out.println(Arrays.deepToString(o));
		}
		static long modulo(long a,long b,long c) {
			long x=1;
			long y=a;
			while(b > 0){
				if(b%2 == 1){
					x=(x*y)%c;
				}
				y = (y*y)%c; // squaring the base
				b /= 2;
			}
			return  x%c;
		}
		static void catalan_numbers(int n) {
			long catalan[]= new long[n+1];
		    catalan[1] = 1;
		    for (int i = 2; i <= n; i++) {
		        for (int j = 1; j <= i - 1; j++) {
		            catalan[i] = catalan[i] + ((catalan[j]) * catalan[i - j]);
		        }
		    }
		}
		
		static ArrayList<Integer> primeFactors(int n)   // O(sqrt(n))
		{
		    // Print the number of 2s that divide n
			ArrayList<Integer> arr= new ArrayList<>();
		    while (n%2 == 0)
		    {
		        arr.add(2);
		        n = n/2;
		    }
		 
		    // n must be odd at this point.  So we can skip one element (Note i = i +2)
		    for (int i = 3; i <= Math.sqrt(n); i = i+2)
		    {
		        // While i divides n, print i and divide n
		        while (n%i == 0)
		        {
		            arr.add(i);
		            n = n/i;
		        }
		    }
		    // This condition is to handle the case when n is a prime number
		    // greater than 2
		    if (n > 2)
		        arr.add(n);
		    
		    return arr;
		}
	
		public static int expo(int a, int b){
			if (b==0)
		        return 1;
		    if (b==1)
		        return a;
		    if (b==2)
		        return a*a;
	
		    if (b%2==0){
		            return expo(expo(a,b/2),2);
		    }
		    else{
		        return a*expo(expo(a,(b-1)/2),2);
		    }
		}
		static class Pair implements Comparable<Pair>
		{
			int f;
			int s;
			Pair(int ii, int cc)
			{
				f=ii;
				s=cc;
			}
			
			public int compareTo(Pair o) 
			{
				return Integer.compare(this.f, o.f);
			}
			
		}
		
		public static int[] sieve(int N){    // O(n*log(logn))
			int arr[]= new int[N+1];
			for(int i=2;i<=Math.sqrt(N);i++){
				if(arr[i]==0){
					for(int j= i*i;j<= N;j= j+i){
						arr[j]=1;
					}
				}
			}
			return arr;
			// All the i for which arr[i]==0 are prime numbers.
		}
		static long gcd(long a,long b){       // O(logn)
			if(b==0) return a;
			return gcd(b,a%b);
		}
		static class InputReader {
	
		    public BufferedReader reader;
		    public StringTokenizer tokenizer;
	 
		    public InputReader(InputStream inputstream) {
		      reader = new BufferedReader(new InputStreamReader(inputstream));
		      tokenizer = null;
		    }
		    
		    public String nextLine(){
		    	String fullLine=null;
		    	while (tokenizer == null || !tokenizer.hasMoreTokens()) {
		            try {
		              fullLine=reader.readLine();
		            } catch (IOException e) {
		              throw new RuntimeException(e);
		            }
		            return fullLine;
		          }
		          return fullLine;
		    }
			public String next() {
		      while (tokenizer == null || !tokenizer.hasMoreTokens()) {
		        try {
		          tokenizer = new StringTokenizer(reader.readLine());
		        } catch (IOException e) {
		          throw new RuntimeException(e);
		        }
		      }
		      return tokenizer.nextToken();
		    }
			public long nextLong() {
			      return Long.parseLong(next());
			    }
		    public int nextInt() {
		      return Integer.parseInt(next());
		    }
		  }
	}