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.util.ArrayList;
import java.util.InputMismatchException;

public class RobbersWatch {
    public static void main(String[] args) {
        FasterScanner sc = new FasterScanner();
        
        int N = sc.nextInt();
        int M = sc.nextInt();
        
        int hrDig = digitsReq(N - 1);
        int mnDig = digitsReq(M - 1);
        
        if (hrDig + mnDig > 7) {
        	System.out.println(0);
        	return;
        }
        
        int cnt = 0;
        int[][] perms = getPermutations(7, hrDig + mnDig);
        for (int[] p : perms) {
        	int hr = 0;
        	int mn = 0;
        	for (int i = 0; i < hrDig; i++) {
        		hr = (hr * 7 + p[i]);
        	}
        	for (int i = hrDig; i < p.length; i++) {
        		mn = (mn * 7 + p[i]);
        	}
        	if (hr < N && mn < M) {
        		cnt++;
        	}
        }
        System.out.println(cnt);
    }
    
    private static int digitsReq(int N) {
    	int cnt = 0;
    	while (N > 0) {
    		N /= 7;
    		cnt++;
    	}
    	return Math.max(1, cnt);
    }

    /**
     * Method for computing permutations.  Returns an array of int[], which contains k elements from { 0, 1, ..., n - 1 }.
     * Each int[] will be sorted in ascending order, and they array of int[] will be sorted ascending as well.
     */
    public static int[][] getPermutations(int n, int k)
    {
        ArrayList<int[]> lst = new ArrayList<int[]>();
        int[] arr = new int[k];
        boolean[] used = new boolean[n];
        permutationHelper(n, k, 0, arr, used, lst);
        return lst.toArray(new int[0][]);
    }

    /**
     * Helper method for computing permutations.  Used by GetPermutations(...).
     */
    private static void permutationHelper(int n, int k, int t, int[] curr, boolean[] used, ArrayList<int[]> lst)
    {
        if (t == k)
        {
            int[] copy = new int[k];
            System.arraycopy(curr, 0, copy, 0, k);
            lst.add(copy);
            return;
        }

        for (int i = 0; i < n; i++)
        {
            if (used[i])
                continue;

            curr[t] = i;
            used[i] = true;
            permutationHelper(n, k, t + 1, curr, used, lst);
            used[i] = false;
        }
    }

	public static class FasterScanner {
		private byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;

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

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

		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 long nextLong() {
			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 - '0';
				c = read();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

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

		public int[] nextIntArray(int n) {
			int[] arr = new int[n];
			for (int i = 0; i < n; i++) {
				arr[i] = nextInt();
			}
			return arr;
		}

		public int[] nextIntArray(int n, int offset) {
			int[] arr = new int[n + offset];
			for (int i = 0; i < n; i++) {
				arr[i + offset] = nextInt();
			}
			return arr;
		}

		public long[] nextLongArray(int n) {
			long[] arr = new long[n];
			for (int i = 0; i < n; i++) {
				arr[i] = nextLong();
			}
			return arr;
		}

		public long[] nextLongArray(int n, int offset) {
			long[] arr = new long[n + offset];
			for (int i = 0; i < n; i++) {
				arr[i + offset] = nextLong();
			}
			return arr;
		}

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

		private boolean isEndOfLine(int c) {
			return c == '\n' || c == '\r' || c == -1;
		}
	}
}