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).
/**
 * Created by ankeet on 6/23/16.
 */
import java.io.*;
import java.util.*;
import java.math.*;

public class C686 {

    public static BufferedReader read = null;
    public static PrintWriter out = null;
    public static StringTokenizer token = null;

    public static long tobase7(long n)
    {
        return Long.parseLong(Long.toString(n,7));
    }

    public static void solve()
    {

        int ndec = nint();
        int mdec = nint();
        long n = tobase7(ndec-1);
        long m = tobase7(mdec-1);
        //out.println(n);
        //out.println(m);
        int nd = (""+n).length();
        int md = (""+m).length();
        long ans = 0;
        if(nd + md > 7)
        {
            out.println("0");
        }
        else
        {
            for(int i=0; i<ndec; i++)
            {
                long nv = tobase7(i);
                String nw = ""+nv;
                while(nw.length() < nd) nw = "0"+nw;
                for(int j = 0; j<mdec; j++)
                {
                    long mv = tobase7(j);
                    String mw = ""+mv;
                    while(mw.length() < md) mw = "0" + mw;
                    int[] f = new int[7];
                    for(int k = 0; k<nd; k++) f[(int)(nw.charAt(k)- '0')]++;
                    for(int k = 0; k<md; k++) f[(int)(mw.charAt(k)- '0')]++;
                    boolean works = true;
                    for(int k = 0; k<7; k++)
                    {
                        if(f[k]>1) works = false;
                    }
                    if(works) ans++;
                }
            }
            out.println(ans);
        }

    }



    public static void main(String[] args)
    {
        read = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        solve();
        out.flush();
        out.close();

    }

    // i/o functions
    public static String next() // returns the next string
    {
        while(token == null || !token.hasMoreTokens())
        {
            try {
                token = new StringTokenizer(read.readLine());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return token.nextToken();
    }

    public static int nint()
    {
        return Integer.parseInt(next());
    }
    public static long nlong()
    {
        return Long.parseLong(next());
    }
    public static double ndouble()
    {
        return Double.parseDouble(next());
    }
    public static int[] narr(int n)
    {
        int[] a = new int[n];
        for(int i=0; i<n; i++) a[i] = nint();
        return a;
    }

    public static long[] nal(int n)
    {
        long[] a = new long[n];
        for(int i=0; i<n; i++) a[i] = nlong();
        return a;
    }


}



