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.util.Scanner;

/**
 * Created on 23.06.2016.
 */
public class task3 {
    private static String from10to7(long number) {
        long value = number;
        String result = "";
        while ( value > 0 ) {
            long p = value / 7;
            long q = value % 7;
            result = q + result;
            value = p;
        }
        if (result.isEmpty())result="0";
        return ( result );
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        long n=in.nextInt();
        long m=in.nextInt();
        String n7=from10to7(n-1);
        String m7=from10to7(m-1);
        int krn=n7.length();
        int krm=m7.length();
        int rez=0;
        int vnak=0;
        String snak="";
        if(krn+krm>7)rez=0;
        else
        for (int i1 = 0; i1 < 7; i1++) {
            if(i1>Integer.parseInt(n7.substring(0,1)))break;
            for (int i2 = 0; i2 < 7; i2++) {
                snak=Integer.toString(i1)+Integer.toString(i2);
                if(krn+krm==2){
                    if(Integer.parseInt(snak.substring(0,krn))>Integer.parseInt(n7))break;
                    if(Integer.parseInt(snak.substring(krn,krn+krm))>Integer.parseInt(m7))break;
                }
                if(i2==i1)continue;
                if(krn+krm==2){rez++; continue;}
                for (int i3 = 0; i3 < 7; i3++) {
                    snak=Integer.toString(i1)+Integer.toString(i2)+Integer.toString(i3);
                    if(krn+krm==3){
                        if(Integer.parseInt(snak.substring(0,krn))>Integer.parseInt(n7))break;
                        if(Integer.parseInt(snak.substring(krn,krn+krm))>Integer.parseInt(m7))break;
                    }
                    if(i3==i1||i3==i2)continue;
                    if(krn+krm==3){rez++; continue;}
                    for (int i4 = 0; i4 < 7; i4++) {
                        snak=Integer.toString(i1)+Integer.toString(i2)+Integer.toString(i3)+Integer.toString(i4);
                        if(krn+krm==4){
                            if(Integer.parseInt(snak.substring(0,krn))>Integer.parseInt(n7))break;
                            if(Integer.parseInt(snak.substring(krn,krn+krm))>Integer.parseInt(m7))break;
                        }
                        if(i4==i1||i4==i2||i4==i3)continue;
                        if(krn+krm==4){rez++; continue;}
                        for (int i5 = 0; i5 < 7; i5++) {
                            snak=Integer.toString(i1)+Integer.toString(i2)+Integer.toString(i3)+Integer.toString(i4)+Integer.toString(i5);
                            if(krn+krm==5){
                                if(Integer.parseInt(snak.substring(0,krn))>Integer.parseInt(n7))break;
                                if(Integer.parseInt(snak.substring(krn,krn+krm))>Integer.parseInt(m7))break;
                            }
                            if(i5==i1||i5==i2||i5==i3||i5==i4)continue;
                            if(krn+krm==5){rez++; continue;}
                            for (int i6 = 0; i6 < 7; i6++) {
                                snak=Integer.toString(i1)+Integer.toString(i2)+Integer.toString(i3)+Integer.toString(i4)+Integer.toString(i5)+Integer.toString(i6);
                                if(krn+krm==6){
                                    if(Integer.parseInt(snak.substring(0,krn))>Integer.parseInt(n7))break;
                                    if(Integer.parseInt(snak.substring(krn,krn+krm))>Integer.parseInt(m7))break;
                                }
                                if(i6==i1||i6==i2||i6==i3||i6==i4||i6==i5)continue;
                                if(krn+krm==6){rez++; continue;}
                                for (int i7 = 0; i7 < 7; i7++) {
                                    snak=Integer.toString(i1)+Integer.toString(i2)+Integer.toString(i3)+Integer.toString(i4)+Integer.toString(i5)+Integer.toString(i6)+Integer.toString(i7);
                                    if(krn+krm==7){
                                        if(Integer.parseInt(snak.substring(0,krn))>Integer.parseInt(n7))break;
                                        if(Integer.parseInt(snak.substring(krn,krn+krm))>Integer.parseInt(m7))break;
                                    }
                                    if(i7==i1||i7==i2||i7==i3||i7==i4||i7==i5||i7==i6)continue;
                                    rez++;
                                }
                            }
                        }
                    }
                }
            }
        }
        System.out.println(rez);
    }
}
