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

public class C {

    public static void main(String[] args) {

        MyScanner in = new MyScanner();
        int n = in.nextInt();
        int m = in.nextInt();

        String n7 = to7(n-1);
        String m7 = to7(m-1);

        int n7len = n7.length();
        int m7len = m7.length();

        if(n7len==0){
            n7len++;
        }

        if(m7len==0){
            m7len++;
        }

        int length = n7len+m7len;

//        System.out.println(" length " + length);
        if(n==1&&m==1){
            System.out.println(0);
            return;
        }else if(length>7){
            System.out.println(0);
            return;
        }else{

            int[] res = new int[length];
            int result = count(n,m,n7len,m7len,new int[]{0,1,2,3,4,5,6},new boolean[7],res,0);
            System.out.println(result);
        }
    }

    private static int count(int n, int m, int nlength, int mlength, int[] array,boolean[] used,int[] res, int index) {
        int result = 0;
//        System.out.println(res[0]+" "+res[1]);
        if(index==res.length){
            int hours = 0;
            int minutes = 0;
            int pow = 1;
            for(int i=nlength-1;i>=0;--i){
                hours+=res[i]*pow;
                pow*=7;
            }
            pow = 1;
            for(int i=mlength+nlength-1;i>=nlength;--i){
                minutes+=res[i]*pow;
                pow*=7;
            }

            if(hours<n&&minutes<m){
//                System.out.println(res[0]+" "+res[1]+" "+res[2]);
//                System.out.println(hours+" "+minutes);
                return 1;
            }else{
                return 0;
            }
        }

        for(int i=0;i<array.length;++i){
            if(!used[i]){
                used[i] = true;
                res[index] = array[i];
                result+=count(n,m,nlength,mlength,array,used,res,index+1);
                used[i] = false;
            }
        }
//        for(int j=index;j<array.length;++j){
//            swap(array,index,j);
//            result+=count(n,m,nlength,mlength,array,index+1);
//            swap(array,index,j);
//        }
        return result;
    }

    private static void swap(int[] array, int index, int j) {
        int tmp = array[index];
        array[index] = array[j];
        array[j] = tmp;
    }

    static String to7(int i){
        StringBuffer result = new StringBuffer();
        while(i>0){
            result.append(i%7);
            i/=7;
        }
        return result.reverse().toString();
    }



    //-----------PrintWriter for faster output---------------------------------
    public static PrintWriter out = new PrintWriter(System.out);

    //-----------MyScanner class for faster input----------
    public static class MyScanner {
        BufferedReader br;
        StringTokenizer st;

        public MyScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

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

        long nextLong() {
            return Long.parseLong(next());
        }

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

        String nextLine(){
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

    }
    //--------------------------------------------------------


//
//    insert -851839419
//    removeMin
//    insert 0
//    removeMin
//    insert -624068418
//    getMin -624068418
//    insert 119194510
//    removeMin
//            removeMin
//    insert 0
//    removeMin
//    insert -179673992
//    getMin -179673992
//    removeMin
//    insert 520596304
//    getMin 520596304
//    insert -265233646
//    removeMin
//    insert -222234500
//    getMin -222234500
//    removeMin
//            removeMin
//    insert 608830643
//    getMin 608830643
//    insert 483106217
//    getMin 483106217
//    insert -597593284
//    getMin -597593284
//    removeMin
//    insert 940783
//    getMin 940783
//    insert -948359037

}