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.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.Set;

public class Main {
	public static void main(String[] args) throws Exception {
		Reader reader = new Reader(System.in);
		// local
		// reader = new Reader(new FileInputStream("input.txt"));

		int T = 1;

		for (int t = 0; t < T; t++) {
			String answer = "No";

			long n = reader.nextInt();
			long m = reader.nextInt();

			n--;
			m--;

			int nLength = Integer.toString(Integer.parseInt("" + n, 10), 7).length();
			int mLength = Integer.toString(Integer.parseInt("" + m, 10), 7).length();

			int totalLength = nLength + mLength;

			int[] digits = { 0, 1, 2, 3, 4, 5, 6 };

			if (totalLength > 7) {
				answer = "0";
			} else {
				// calculate 7Ptotal length
				Combination c = new Combination(7, totalLength);
				int counter = 0;
				while (c.hasNext()) {
					int[] next = c.next();
					String str = Arrays.toString(next);
					str = str.replace(",", "").replace("[", "").replace("]", "").replace(" ", "");
					
					Set<String> perms = permutationFinder(str);
					
					for (String string : perms) {


						int hour = Integer.parseInt(string.substring(0, nLength), 7);
						int minute = Integer.parseInt(string.substring(nLength), 7);
						
						if (hour <= n && minute <= m) {
							counter++;
						}
						

					}
				}
				answer = "" + counter;

			}

			System.out.print(answer + "\n");
		}
	}

	public static String charInsert(String str, char c, int j) {
		String begin = str.substring(0, j);
		String end = str.substring(j);
		return begin + c + end;
	}

	public static Set<String> permutationFinder(String str) {
		Set<String> perm = new HashSet<String>();
		// Handling error scenarios
		if (str == null) {
			return null;
		} else if (str.length() == 0) {
			perm.add("");
			return perm;
		}
		char initial = str.charAt(0); // first character
		String rem = str.substring(1); // Full string without first character
		Set<String> words = permutationFinder(rem);
		for (String strNew : words) {
			for (int i = 0; i <= strNew.length(); i++) {
				perm.add(charInsert(strNew, initial, i));
			}
		}
		return perm;
	}

	static class Combination {
		private int n, r;
		private int[] index;
		private boolean hasNext = true;

		public Combination(int n, int r) {
			this.n = n;
			this.r = r;
			index = new int[r];
			for (int i = 0; i < r; i++)
				index[i] = i;
		}

		public boolean hasNext() {
			return hasNext;
		}

		// Based on code from KodersCode:
		// The algorithm is from Applied Combinatorics, by Alan Tucker.
		// Move the index forward a notch. The algorithm finds the rightmost
		// index element that can be incremented, increments it, and then
		// changes the elements to the right to each be 1 plus the element on
		// their left.
		//
		// For example, if an index of 5 things taken 3 at a time is at {0 3 4},
		// only the 0 can
		// be incremented without running out of room. The next index is {1,
		// 1+1, 1+2) or
		// {1, 2, 3}. This will be followed by {1, 2, 4}, {1, 3, 4}, and {2, 3,
		// 4}.

		private void moveIndex() {
			int i = rightmostIndexBelowMax();
			if (i >= 0) {
				index[i] = index[i] + 1;
				for (int j = i + 1; j < r; j++)
					index[j] = index[j - 1] + 1;
			} else
				hasNext = false;
		}

		public int[] next() {
			if (!hasNext)
				return null;
			int[] result = new int[r];
			for (int i = 0; i < r; i++)
				result[i] = index[i];
			moveIndex();
			return result;
		}

		// return int,the index which can be bumped up.
		private int rightmostIndexBelowMax() {
			for (int i = r - 1; i >= 0; i--)
				if (index[i] < n - r + i)
					return i;
			return -1;
		}
	}

	static class Reader {

		private InputStream stream;
		private byte[] buf = new byte[1024 * 1024];
		private int curChar;
		private int numChars;

		public Reader(InputStream stream) {
			this.stream = stream;
		}

		public int read() {
			if (numChars == -1)
				throw new InputMismatchException();
			if (curChar >= numChars) {
				curChar = 0;
				try {
					numChars = stream.read(buf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (numChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}

		public long nextInt() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			long res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c & 15;
				c = read();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

		public String nextString() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		public static boolean isSpaceChar(int c) {
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

	}
}