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 ≤ 10^9) — 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).

Difficulty: competition

Starter code:

Solutions:
from itertools import *
def f(x):
    x -= 1
    ret = 0
    if x == 0:
        ret = 1
    else:
        while x != 0:
            ret += 1
            x //= 7
    return ret

def g(d):
    ret = 0
    for v in d:
        ret = ret * 7 + v
    return ret



n, m = list(map(int, input().split()))
a = f(n)
b = f(m)
if a + b > 7:
    print(0)
else:
    ans = 0
    for p in permutations(list(range(7)), a + b):
        if g(p[:a]) < n and g(p[a:]) < m:
            ans += 1
    print(ans)




def D(n):
	x,r=n-1,1
	while x>=7:
		x//=7
		r+=1
	return r
def H(n,l):
	x,r=n,""
	if x==0:r+='0'
	while x:
		r+=chr(ord('0')+x%7)
		x//=7
	r+='0'*(l-len(r))
	return r
a,b=list(map(int,input().split()))
la=D(a)
lb=D(b)
V=[0]*99
r=0
def F(deep,wa,wb):
	nonlocal r
	if wa>=a or wb>=b:return
	if deep==la+lb:
		r+=1
		return
	i=-1
	while i<6:
		i+=1
		if V[i]:continue
		V[i]=1
		if deep>=la:
			F(deep+1,wa,wb+i*(7**(lb-1-(deep-la))))
		else:
			F(deep+1,wa+i*(7**(la-1-deep)),wb)
		V[i]=0
if la+lb<8:F(0,0,0)
print(r)


def sev(n):
    if n == 0:
        return 1
    m = 0
    while n > 0:
        n //= 7
        m += 1
    return m

def fil(n, m, l, r):
    d = []
    for i in range(r):
        d += [m % 7]
        m //= 7
    for i in range(l):
        d += [n % 7]
        n //= 7
    
    if len(set(d)) != l + r:
        return 0
    return 1

n, m = list(map(int, input().split()))
l, r = sev(n - 1), sev(m - 1)

if l + r > 7:
    print(0)
else:
    c = 0
    for i in range(n):
        for j in range(m):
            c += fil(i, j, l, r)
    print(c)


from itertools import permutations

def str_base(number,base):
    if number < 0:
        return '-' + str_base(-number, base)
    (d, m) = divmod(number, base)
    if d > 0:
        return str_base(d, base) + str(m)
    return str(m)

n, m = map(int, input().split())

max_h = str_base(n-1, 7)
max_m = str_base(m-1, 7)
len_h = len(max_h)
len_m = len(max_m)

result = 0
for p in permutations(list(range(7)) , len_h + len_m):
    if str(''.join(map(str, p[:len_h]))) <= max_h and str(''.join(map(str, p[len_h:]))) <= max_m:
        result += 1
        
print(result)

from itertools import permutations
from math import ceil, log

n, m = t = list(map(int, input().split()))
l, _ = t = [ceil(log(x, 7.)) if x > 1 else 1 for x in t]
print(sum(int(s[:l], 7) < n and int(s[l:], 7) < m for s in map(''.join, permutations("0123456", sum(t)))))


BASE = 7

def itov(x):
    digits = []
    if x == 0:
        digits.append(0)
    while x > 0:
        digits.append(x % BASE)
        x //= BASE
    digits.reverse()
    return digits

def gen(pos = 0, minute = False, smaller = False):
    max_val = max_minute if minute else max_hour
    if pos >= len(max_val):
        if minute:
            return 1
        else:
            return gen(0, True)
    else:
        ans = 0
        for digit in range(BASE):
            if not used[digit] and (smaller or digit <= max_val[pos]):
                used[digit] = True
                ans += gen(pos + 1, minute, smaller or digit < max_val[pos])
                used[digit] = False
        return ans

n, m = map(int, input().split())
n -= 1
m -= 1
used = [False] * BASE
max_hour = itov(n)
max_minute = itov(m)
print(gen())

from itertools import permutations
from math import ceil, log

nn, m = t = list(map(int, input().split()))
l, _ = t = [ceil(log(x, 7.)) if x > 1 else 1 for x in t]
print(sum(int(s[:l], 7) < nn and int(s[l:], 7) < m for s in map(''.join, permutations("0123456", sum(t)))))


from collections import Counter

def getd(n):
    v = 1
    while n > 7 ** v:
        v += 1
    return v

def getb(n, d):
    v = 0
    for i in range(d):
        b = 1 << (n % 7)
        if v & b:
            return 0
        v |= b
        n //= 7
    return v

def getc(n, d):
    c = Counter()
    for i in range(n):
        cb = getb(i, d)
        if cb:
            c[cb] += 1
    return c

x, y = map(int, input().split())
xd, yd = getd(x), getd(y)
if xd + yd > 7:
    print(0)
    return
xc, yc, v = getc(x, xd), getc(y, yd), 0
for xi in xc:
    for yi in yc:
        if xi & yi == 0:
            v += xc[xi] * yc[yi]
print(v)

BASE = 7

def itov(x):
    digits = []
    if x == 0:
        digits.append(0)
    while x > 0:
        digits.append(x % BASE)
        x //= BASE
    digits.reverse()
    return digits

def gen(pos = 0, minute = False, smaller = False):
    max_val = max_minute if minute else max_hour
    if pos >= len(max_val):
        if minute:
            return 1
        else:
            return gen(0, True)
    else:
        ans = 0
        for digit in range(BASE):
            if not used[digit] and (smaller or digit <= max_val[pos]):
                used[digit] = True
                ans += gen(pos + 1, minute, smaller or digit < max_val[pos])
                used[digit] = False
        return ans

n, m = map(int, input().split())
n -= 1
m -= 1
used = [False] * BASE
max_hour = itov(n)
max_minute = itov(m)
print(gen())

from itertools import permutations
n, m = map(int, input().split())
t1 = 1; t2 = 1;
while 7 ** t1 < n: t1 += 1
while 7 ** t2 < m: t2 += 1
count = 0

for perm in permutations(list('0123456'), t1 + t2):
        a = int(''.join(perm[:t1]), 7)
        b = int(''.join(perm[t1:t1 + t2]), 7)
        if a < n and b < m:
            count += 1
print(count)

from itertools import permutations
def dfs(x):
    r = x==0
    while x :
        r += 1
        x //= 7
    return r
n ,m = list(map(int,input().split()))
res, ln, lm = 0, dfs(n-1), dfs(m-1)
for i in permutations('0123456', ln+lm):
    i = ''.join(i)
    res += int(i[:ln], 7) < n and int(i[ln:], 7) < m
print(res)


from itertools import permutations
def findPow(num):
    ans = 1
    num = num // 7
    while num > 0:
        num //= 7
        ans +=1
    return ans
a, b = [int(x) for x in input().split(' ')]
x = findPow(a-1)
y = findPow(b-1)
ans = 0
if (x + y) > 7:
    ans = 0
else:
    for i in permutations('0123456', x+y):
        s = ''.join(list(i))
        p, q = int(s[:x], 7), int(s[x:], 7)
        if p <= a-1 and q <= b-1:
            ans+=1

print(ans)  


import math

def digits(x, k):
    d = [False] * 7
    for i in range(k):
        if d[x%7]:
            return False
        d[x%7] = True
        x //= 7
    return True
    
def solve(n, m):
    if n*m > 7**7:
        return 0
    k1 = math.ceil(math.log(n, 7)) if n > 1 else 1
    k2 = math.ceil(math.log(m, 7)) if m > 1 else 1
    s = 0
    for i in range(n):
        for j in range(m):
            s += digits(i * 7 ** k2 + j, k1 + k2)
    return s


n, m = list(map(int, input().split()))
print(solve(n, m))


from itertools import *
from math import *
n, m = map(int, input().split())
x, y = (max(1, ceil(log(k, 7))) for k in [n, m])
p = [''.join(t) for t in permutations('0123456', x + y)]
print(0 if x + y > 7 else sum(int(t[:x], 7) < n and int(t[x:], 7) < m for t in p))

from itertools import permutations as P

n, m = list(map(int, input().split()))
a = n-1
b = m-1
x = 0
y = 0
c = 0
while a:
	x += 1
	a = a//7
if x == 0:
	x = 1
while b:
	y += 1
	b = b//7
if y == 0:
	y = 1
if x+y > 7 or x+y == 0:
 	print(0)
else:
	r = x+y
	l = list(P('0123456', r))
	for ele in l:
		a = 0
		b = 0
		for i in range(x):
			a += int(ele[i])*(7**(x-i-1))
		for i in range(x, r):
			b += int(ele[i])*(7**(r-i-1))
		if a < n and b < m:
			c += 1
			#print(a, b, end = ' ')
			#print()
	print(c)


from itertools import permutations
from math import ceil, log

n, m = t = list(map(int, input().split()))
l, _ = t = [ceil(log(x, 7.)) if x > 1 else 1 for x in t]
print(sum(int(s[:l], 7) < n and int(s[l:], 7) < m for s in map(''.join, permutations("0123456", sum(t)))))




# Made By Mostafa_Khaled


import math
import sys
from bisect import bisect_right, bisect_left, insort_right
from collections import Counter, defaultdict
from heapq import heappop, heappush
from itertools import accumulate, permutations, combinations
from sys import stdout

R = lambda: map(int, input().split())
n, m = R()
nc, mc = n, m
n -= 1
m -= 1
a = b = 0
while n:
    n //= 7
    a += 1
while m:
    m //= 7
    b += 1
a, b = max(1, a), max(1, b)
if a + b > 7:
    print(0)
else:
    res = 0
    bs = [1]
    whole = [0, 1, 2, 3, 4, 5, 6]
    for i in range(10):
        bs.append(bs[-1] * 7)
    for pa in permutations(whole, a):
        if sum(x * y for x, y in zip(bs, pa)) < nc:
            for pb in permutations([x for x in whole if x not in pa], b):
                if sum(x * y for x, y in zip(bs, pb)) < mc:
                    res += 1
    print(res)

from itertools import permutations as p
def f(n, tn):
    if not n:
        tn.append(0)
        return 1
    cnt = 0
    while n:
        tn.append(n % 7)
        n //= 7
        cnt += 1
    return cnt
ans = 0
tn, tm = [], []
n, m = map(int, input().split())
n -= 1
m -= 1
x, y = f(n, tn), f(m, tm)
tn, tm = tuple(tn[::-1]), tuple(tm[::-1])

s = {0, 1, 2, 3, 4, 5, 6}
if x + y > 7:
    print(0);return
for i in p(s, x):
    if i <= tn:
        for j in p(s - set(i), y):
            if j <= tm:
                ans += 1
print(ans)

from itertools import permutations as p


def length_base_7(ele, arr):
    if not ele:
        arr.append(0)
        return 1
    length = 0
    while ele:
        arr.append(ele % 7)
        ele //= 7
        length += 1
    return length


n, m = list(map(int, input().split()))
n -= 1
m -= 1

n_base7, m_base7 = [], []
n_length, m_length = length_base_7(n, n_base7), length_base_7(m, m_base7)

n_base7, m_base7 = n_base7[::-1], m_base7[::-1]

combination_number = {0, 1, 2, 3, 4, 5, 6}
res = 0
for i in p(combination_number, n_length):
    if list(i) <= n_base7:
        for j in p(combination_number - set(i), m_length):
            if list(j) <= m_base7:
                res += 1

print(res)


import sys
import math
from itertools import permutations
def convert(n):
    if n == 0:
        return []
    return convert(n//7)+[n % 7]
n,m = list(map(int,sys.stdin.readline().split()))
x = convert(n-1)
y = convert(m-1)
nn = max(1,len(x))
mm = max(1,len(y))
ans = 0
for i in permutations("0123456",nn+mm):
    a, b = "".join(i[:nn]), "".join(i[nn:])
    ans += (int(a,7)<n and int(b,7)<m)
sys.stdout.write(str(ans))






def digits(n):
    l=[]
    if(n==0):
        l.append(0)
    else:
        while(n>0):
            l.append(n%7)
            n=n//7
        l.reverse()
    return l
l=input().split()
n=int(l[0])
m=int(l[1])
numh=len(digits(n-1))
numm=len(digits(m-1))
ans=0
if(numh+numm>7):
    ans=0
else:
    for i in range(n):
        for j in range(m):
            o=i
            p=j
            used=[0 for lo in range(7)]
            k=0
            while(k<numh):
                used[o%7]+=1
                o=o//7
                k+=1
            k=0
            while(k<numm):
                used[p%7]+=1
                p=p//7
                k+=1
            if(max(used)==1):
                ans+=1
print(ans)

from itertools import *
from math import *
n, m = map(int, input().split())
x = ceil(log(m,7))
y = ceil(log(n,7))
ms = dict()
x = max(1,x)
y = max(1,y)
def conv(s):
	s = s[::-1]
	j = 0
	num = 0
	for i in s:
		num += int(i)*7**j
		j+=1
	# print(num)
	return num
if x+y > 7:
	print(0)
else:
	s = '0123456'
	ans = 0
	for i in permutations(s,x+y):
		# print(i)
		
		a = i[:y]
		b = i[y:x+y]
		# print(a,b)
		if len(a) == 0 or len(b) == 0:
			continue
		if conv(a)<n and conv(b)<m:
			# print(conv(a),conv(b),i)
			ans +=1
	print(ans)