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.*;

public class C686 {
	
	static int l, c, la, lb;
	static long x, y;
	
	public static void gen(int i, int[] array) {
		if(i == l) {
			String a = "", b = "";
			int[] count = new int[7];
			for(int j = 0; j < la; j++) {
				a = ("" + array[j]) + a;
				count[array[j]]++;
			}
			for(int j = la; j < la + lb; j++) {
				b = ("" + array[j]) + b;
				count[array[j]]++;
			}
			long u = Long.parseLong(a, 7), v = Long.parseLong(b, 7);
			boolean flag = true;
			for(int j = 0; j < 7; j++)
				if(count[j] > 1) {
					flag = false;
					break;
				}
			
			if(u <= x && v <= y && flag)
				c++;
			
			return;
		}
		
		for(int j = 0; j < 7; j++) {
			array[i] = j;
			gen(i + 1, array);
		}
	}
	
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		PrintWriter pw = new PrintWriter(System.out);
		
		int n = sc.nextInt() - 1, m = sc.nextInt() - 1;
		String a = "", b = "";
		while(n > 0) {
			a = ("" + (n % 7)) + a;
			n /= 7;
		}
		
		while(m > 0) {
			b = ("" + (m % 7)) + b;
			m /= 7;
		}
		
		if(a.equals(""))
			a = "0";
		if(b.equals(""))
			b = "0";
		
		l = a.length() + b.length();
		if(l > 7) {
			pw.println(0);
		} else {
			la = a.length();
			lb = b.length();
			x = Long.parseLong(a, 7);
			y = Long.parseLong(b, 7);
			c = 0;
			int[] array = new int[l];
			gen(0, array);
			pw.println(c);
		}
		
		pw.flush();
	}

	public static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream system) {
			br = new BufferedReader(new InputStreamReader(system));
		}

		public Scanner(String file) throws Exception {
			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 String nextLine() throws IOException {
			return br.readLine();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public char nextChar() throws IOException {
			return next().charAt(0);
		}

		public Long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

		public void waitForInput() throws InterruptedException {
			Thread.sleep(3000);
		}
	}
}
