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.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskA solver = new TaskA();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskA {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            int m = in.nextInt();
            int nLen = 0;
            int mLen = 0;
            int nf = n;
            int mf = m;
            int nr = 0;
            int mr = 0;
            while (nf > 0) {
                nr += nf % 7;
                nf /= 7;
                nLen++;
            }
            if (nr == 1 && nLen > 1) nLen--;
            while (mf > 0) {
                mr += mf % 7;
                mf /= 7;
                mLen++;
            }
            if (mr == 1 && mLen > 1) mLen--;
            if (mLen + nLen > 7) out.println(0);
            else {
                int ans = 0;
                int[] digits = new int[mLen + nLen];
                for (int h = 0; h < n; h++) {
                    for (int mt = 0; mt < m; mt++) {
                        int cur = 0;
                        nf = h;
                        mf = mt;
                        for (int i = 0; i < nLen; i++) {
                            digits[cur++] = nf % 7;
                            nf /= 7;
                        }
                        for (int i = 0; i < mLen; i++) {
                            digits[cur++] = mf % 7;
                            mf /= 7;
                        }
                        loop:
                        {
                            for (int i = 0; i < digits.length; i++) {
                                for (int j = i + 1; j < digits.length; j++) {
                                    if (digits[i] == digits[j]) break loop;
                                }
                            }
                            ans++;
                        }
                    }
                }
                out.println(ans);
            }
        }

    }

    static class InputReader {
        private StringTokenizer tokenizer;
        private BufferedReader reader;

        public InputReader(InputStream inputStream) {
            reader = new BufferedReader(new InputStreamReader(inputStream));
        }

        private void fillTokenizer() {
            if (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }

        public String next() {
            fillTokenizer();
            return tokenizer.nextToken();
        }

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

    }
}

