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).

Source: codeforces | Difficulty: MEDIUM_HARD
Tags: Dynamic programming

Combinatorics

Mathematics

Complete search


Solutions:
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)


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
from math import ceil, log
(nn, m) = t = list(map(int, input().split()))
(l, _) = t = [ceil(log(x, 7.0)) 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 sys import exit
from itertools import permutations as prm

def to_base(n, base):
	ans = []
	while n // base > 0:
		ans.append(n % base)
		n //= base
	ans.append(n % base)
	ans.reverse()
	return ans

def le(l, n, m):
	prev_eq = True
	for i in range(len(n)):
		if l[i] == n[i]:
			continue
		if l[i] > n[i]:
			return False
		else:
			break
	for i in range(len(m)):
		if l[i + len(n)] == m[i]:
			continue
		if l[i + len(n)] > m[i]:
			return False
		else:
			break
	return True
(n, m) = map(lambda x: to_base(int(x) - 1, 7), input().split())
ln = len(n)
lm = len(m)
if ln + lm > 7:
	print(0)
	exit(0)
ans = 0
for i in prm(range(7), ln + lm):
	if le(i, n, m):
		ans += 1
print(ans)


from sys import stdin, stdout
from math import log10 as lg
(n, m) = map(int, stdin.readline().split())
Num = 7
hours = []
minutes = []
challengers = []
ans = 0

def pref(a, maxlen):
	if len(a) == maxlen:
		challengers.append(a[:])
	elif not len(a):
		for i in range(1, Num):
			if i not in a:
				a.append(i)
				pref(a, maxlen)
				a.pop()
	else:
		for i in range(Num):
			if i not in a:
				a.append(i)
				pref(a, maxlen)
				a.pop()
for i in range(1, Num):
	pref([], i)
challengers.append([0])
for i in challengers:
	hours.append((int(lg(max(1, n - 1)) / lg(7)) - len(i) + 1) * '0' + ''.join(list(map(str, i))))
	if hours[-1].count('0') > 1:
		hours.pop()
	minutes.append((int(lg(max(m - 1, 1)) / lg(7)) - len(i) + 1) * '0' + ''.join(list(map(str, i))))
	if minutes[-1].count('0') > 1:
		minutes.pop()
lenh = len(hours)
lenm = len(minutes)
for i in range(lenh):
	cnt = 0
	vl = str(hours[i])[::-1]
	for j in range(len(vl)):
		if int(vl[j]):
			cnt += int(vl[j]) * 7 ** j
	if cnt < n:
		hours.append(hours[i])
for i in range(lenm):
	cnt = 0
	vl = str(minutes[i])[::-1]
	for j in range(len(vl)):
		if int(vl[j]):
			cnt += int(vl[j]) * 7 ** j
	if cnt < m:
		minutes.append(minutes[i])
hours = hours[lenh:]
minutes = minutes[lenm:]
for f in hours:
	for s in minutes:
		if not set(list(str(f))) & set(list(str(s))):
			ans += 1
stdout.write(str(ans))


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)


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

def digitos(a):
	b = 7
	r = 1
	while b < a:
		b *= 7
		r += 1
	return r

def valido(s):
	return int(s[:dn], 7) < n and int(s[dn:], 7) < m

def solve(s):
	if len(s) == dn + dm:
		if valido(s):
			resp.append(s)
	for x in '0123456':
		if x not in s:
			solve(s + x)
dn = digitos(n)
dm = digitos(m)
resp = []
if dn + dm > 7:
	print('0')
else:
	solve('')
	print(len(resp))


import sys
input = sys.stdin.readline
from itertools import permutations
from math import *

def convert(p, pp, n):
	h = bin(p)[2:]
	h = '0' * (7 - len(h)) + h
	s = []
	c = 0
	if h.count('1') != pp:
		return 0
	for i in range(7):
		if h[i] == '1':
			s.append(str(6 - i))
	if len(s) == 0:
		return 0
	perm = permutations(s)
	perm = list(perm)
	perm.sort()
	for j in perm:
		z = int(''.join(j), 7)
		if z < n:
			c += 1
		else:
			break
	return c
(n, m) = map(int, input().split())
c = 0
pp = 0
y = 1
while y < n:
	y *= 7
	pp += 1
pp = max(pp, 1)
qq = 0
y = 1
while y < m:
	y *= 7
	qq += 1
qq = max(qq, 1)
for i in range(1, 2 ** 7):
	for j in range(1, 2 ** 7):
		p = convert(i, pp, n)
		q = convert(j, qq, m)
		if p > 0 and q > 0 and (i & j == 0):
			c += p * q
print(c)


import itertools

def baseN(num, b, numerals='0123456789abcdefghijklmnopqrstuvwxyz'):
	return num == 0 and numerals[0] or baseN(num // b, b, numerals).lstrip(numerals[0]) + numerals[num % b]

def main():
	(n, m) = [int(x) for x in input().split()]
	(n7, m7) = (baseN(n - 1, 7), baseN(m - 1, 7))
	if len(n7) + len(m7) > 7:
		print(0)
		return
	cnt = 0
	for p in itertools.permutations('0123456', len(n7) + len(m7)):
		(pn, pm) = (''.join(p[:len(n7)]), ''.join(p[len(n7):]))
		if int(pn, 7) < n and int(pm, 7) < m:
			cnt += 1
	print(cnt)
main()


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)


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 collections import Counter
(n, m) = map(int, input().split())

def base7(num):
	numerals = '0123456'
	return num == 0 and numerals[0] or base7(num // 7).lstrip(numerals[0]) + numerals[num % 7]

def unique(s):
	return len(set(s)) == len(s)
max_n_len = len(base7(n - 1))
max_m_len = len(base7(m - 1))
n_combinations = Counter()
m_combinations = Counter()
result = 0
if max_n_len + max_m_len > 7:
	print(0)
else:
	for i in range(n):
		i_b7 = base7(i).zfill(max_n_len)
		if unique(i_b7):
			n_combinations[frozenset(i_b7)] += 1
	for j in range(m):
		j_b7 = base7(j).zfill(max_m_len)
		if unique(j_b7):
			m_combinations[frozenset(j_b7)] += 1
	for (n_combination, n_number) in n_combinations.items():
		for (m_combination, m_number) in m_combinations.items():
			if not n_combination & m_combination:
				result += n_number * m_number
	print(result)


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))


(a, b) = map(int, input().split())

def getdig(x):
	if x == 0:
		return (0, 1)
	s = ''
	c = 0
	while x > 0:
		s = str(x % 7) + s
		c += 1
		x //= 7
	return (int(s), c)
(ass, an) = getdig(a - 1)
(bss, bn) = getdig(b - 1)
tn = an + bn
if tn > 7:
	print(0)
	exit()
count = 0
sor = [str(i) for i in range(7)]

def make(s, list, depth):
	global count
	for x in list:
		new = list[:]
		new.remove(x)
		make(s + x, new, depth + 1)
	if depth == tn:
		(aaa, bbb) = (s[:an], s[an:])
		if int(aaa) <= ass and int(bbb) <= bss:
			count += 1
make('', sor, 0)
print(count)


import math
from itertools import permutations
from itertools import product
a = input().split()
n = int(a[0])
m = int(a[1])

def log(x, y):
	if x < 1:
		return 0
	else:
		return math.log(x, y)
n1 = math.floor(log(n - 1, 7)) + 1
m1 = math.floor(log(m - 1, 7)) + 1
if n1 + m1 > 7:
	print(0)
else:

	def xxx(y):
		y2 = math.floor(log(y, 7)) + 1
		z = ['0' for i in range(y2)]
		for i in range(y2):
			y1 = math.floor(log(y, 7))
			if y1 == y2 - i - 1:
				z[i] = str(y // 7 ** y1)
				y -= 7 ** y1 * int(z[i])
		return z
	n7 = xxx(n - 1)
	m7 = xxx(m - 1)
	n7 = int(''.join(n7))
	m7 = int(''.join(m7))
	vb = set()
	if n1 + m1 == 7:
		for i in permutations(['0', '1', '2', '3', '4', '5', '6'], 7):
			if int(''.join(i[:n1])) > n7:
				pass
			elif int(''.join(i[n1:])) > m7:
				pass
			else:
				b = True
				for j in ['0', '1', '2', '3', '4', '5', '6']:
					if i.count(j) > 1:
						b = False
						break
				if b:
					vb.add(i)
		print(len(vb))
	else:
		for i in product(permutations(['0', '1', '2', '3', '4', '5', '6'], n1), permutations(['0', '1', '2', '3', '4', '5', '6'], m1)):
			if int(''.join(i[0])) > n7:
				pass
			elif int(''.join(i[1])) > m7:
				pass
			else:
				b = True
				for j in ['0', '1', '2', '3', '4', '5', '6']:
					if i[0].count(j) + i[1].count(j) > 1:
						b = False
						break
				if b:
					vb.add(i)
		print(len(vb))


def osn(x):
	ans = []
	x7 = ''
	while x >= 7:
		ans.append(x % 7)
		x = x // 7
	ans.append(x)
	ans.reverse()
	for k in ans:
		x7 += str(k)
	return x7

def it(n, m):
	from itertools import permutations
	ans = 0
	for k in permutations('0123456', len(n) + len(m)):
		s = ''
		for l in k:
			s += l
		if int(s[:len(n)]) <= int(n) and int(s[len(n):]) <= int(m):
			ans += 1
	return ans
(n, m) = map(int, input().split())
(n, m) = (osn(n - 1), osn(m - 1))
if len(n) + len(m) > 7:
	print(0)
else:
	print(it(n, m))


from itertools import permutations
from math import log
from math import ceil
from sys import exit
i = input()
N = int(i[:i.index(' ')])
M = int(i[i.index(' ') + 1:])
l = max(ceil(log(N, 7)), 1)
r = max(ceil(log(M, 7)), 1)
if l + r > 7 or l == 0 or r == 0:
	print(0)
	exit()
ans = []
for x in permutations([0, 1, 2, 3, 4, 5, 6]):
	cnt = 1
	total = 0
	for a in range(l):
		total += cnt * x[a]
		cnt *= 7
	if total >= N:
		continue
	cnt = 1
	total = 0
	for a in range(l, l + r):
		total += cnt * x[a]
		cnt *= 7
	if total >= M:
		continue
	if x[:l + r] not in ans:
		ans += [x[:l + r]]
print(len(ans))


(n, m) = map(int, input().split())
d = [pow(7, i) for i in range(12)]
ans = 0
(nn, mm, kn, km) = (n - 1, m - 1, 0, 0)
while nn:
	kn += 1
	nn //= 7
while mm:
	km += 1
	mm //= 7
kn += kn == 0
km += km == 0
(nn, mm) = ([0] * kn, [0] * km)

def hm(r1, r2, s1, s2):
	global ans
	if s1 >= n:
		return
	if r1 == kn:
		if s2 >= m:
			return
		if r2 == km:
			ans += 1
			return
		for i in range(0, 7):
			mm[r2] = i
			if i in nn:
				continue
			if len(set(mm[:r2 + 1])) < r2 + 1:
				continue
			hm(r1, r2 + 1, s1, s2 + i * d[r2])
		return
	for i in range(0, 7):
		nn[r1] = i
		if len(set(nn[:r1 + 1])) < r1 + 1:
			continue
		hm(r1 + 1, r2, s1 + i * d[r1], s2)
hm(0, 0, 0, 0)
print(ans)


from itertools import permutations

def getdigits(x):
	if x == 0:
		return 1
	digs = 0
	while x != 0:
		x //= 7
		digs += 1
	return digs
(n, m) = map(int, input().split())
(a, b) = (getdigits(n - 1), getdigits(m - 1))
c = a + b
if c > 7:
	print(0)
	exit(0)
combs = permutations(range(7), c)
ans = 0
for comb in combs:
	d = comb[:a]
	e = comb[a:]
	f = ''
	g = ''
	for i in d:
		f += str(i)
	for i in e:
		g += str(i)
	x = int(f, 7)
	y = int(g, 7)
	if x < n and y < m:
		ans += 1
print(ans)


import itertools

def is_valid(p, n):
	num = int(''.join(map(str, p)), 7)
	return 0 <= num < n

def get_sz(x):
	if x == 1:
		return 1
	t = 1
	ans = 0
	while t < x:
		t *= 7
		ans += 1
	return ans
(n, m) = map(int, input().split())
ans = 0
(sz_left, sz_right) = (get_sz(n), get_sz(m))
for i in range(3 ** 7):
	left_num = []
	right_num = []
	cur = i
	for j in range(7):
		if cur % 3 == 0:
			left_num.append(j)
		elif cur % 3 == 1:
			right_num.append(j)
		else:
			pass
		cur //= 3
	if len(left_num) != sz_left or len(right_num) != sz_right:
		continue
	for perm_left in itertools.permutations(left_num):
		if not is_valid(perm_left, n):
			continue
		for perm_right in itertools.permutations(right_num):
			if not is_valid(perm_right, m):
				continue
			ans += 1
print(ans)


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:
	exit(print(0))
for i in p(s, x):
	if i <= tn:
		for j in p(s - set(i), y):
			if j <= tm:
				ans += 1
print(ans)


(n, m) = [int(x) for x in input().split()]
x = 7
d1 = 1
while x < n:
	d1 += 1
	x *= 7
x = 7
d2 = 1
while x < m:
	d2 += 1
	x *= 7
if d1 + d2 > 7:
	print(0)
	exit()
c1 = {}
L = [0] * d1
L[0] = -1
for sadas in range(n):
	L[0] += 1
	for j in range(d1):
		if L[j] == 7:
			L[j] = 0
			L[j + 1] += 1
	g = [0] * 7
	for j in range(7):
		g[j] = len([x for x in L if x == j])
	if max(g) < 2:
		if tuple(g) not in c1:
			c1[tuple(g)] = 0
		c1[tuple(g)] += 1
c2 = {}
L = [0] * d2
L[0] = -1
for sadas in range(m):
	L[0] += 1
	for j in range(d2):
		if L[j] == 7:
			L[j] = 0
			L[j + 1] += 1
	g = [0] * 7
	for j in range(7):
		g[j] = len([x for x in L if x == j])
	if max(g) < 2:
		if tuple(g) not in c2:
			c2[tuple(g)] = 0
		c2[tuple(g)] += 1
ans = 0
for x in c1:
	for y in c2:
		if len([z for z in range(7) if x[z] + y[z] > 1]) == 0:
			ans += c1[x] * c2[y]
print(ans)


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)


from itertools import permutations
(n, m) = tuple(map(int, input().split()))

def baseN(num, b, numerals='0123456789abcdefghijklmnopqrstuvwxyz'):
	return num == 0 and numerals[0] or baseN(num // b, b, numerals).lstrip(numerals[0]) + numerals[num % b]

def lt_eq(a, b):
	if len(a) == 0 and len(b) == 0:
		return True
	elif a[0] > b[0]:
		return False
	elif a[0] < b[0]:
		return True
	else:
		return lt_eq(a[1:], b[1:])
nmax = tuple(map(int, baseN(n - 1, 7)))
mmax = tuple(map(int, baseN(m - 1, 7)))
s = len(nmax) + len(mmax)
ans = 0
if s > 7:
	print(0)
else:
	for c in permutations(range(7), s):
		(left, right) = (c[:len(nmax)], c[len(nmax):])
		if lt_eq(left, nmax) and lt_eq(right, mmax):
			ans += 1
	print(ans)


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(c)


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)


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
	return num
if x + y > 7:
	print(0)
else:
	s = '0123456'
	ans = 0
	for i in permutations(s, x + y):
		a = i[:y]
		b = i[y:x + y]
		if len(a) == 0 or len(b) == 0:
			continue
		if conv(a) < n and conv(b) < m:
			ans += 1
	print(ans)


from itertools import permutations

def find_power(n):
	res = 1
	n = n // 7
	while n > 0:
		n //= 7
		res += 1
	return res
(a, b) = list((int(x) for x in input().split()))
x = find_power(a - 1)
y = find_power(b - 1)
res = 0
if x + y > 7:
	print(0)
	exit()
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:
		res += 1
print(res)


import math

def comb(lst, n, k, used):
	if len(lst) == n:
		yield lst
	else:
		for i in range(k):
			if not used[i]:
				lst.append(i)
				used[i] = True
				yield from comb(lst, n, k, used)
				used[i] = False
				lst.pop()
(n, m) = map(int, input().split())
yach_n = max(math.ceil(math.log(n, 7)), 1)
yach_m = max(math.ceil(math.log(m, 7)), 1)
yach = yach_n + yach_m
amount = 0
for lst in comb([], yach, 7, [False] * 7):
	curr_hours = lst[0]
	j = 1
	while j < yach_n:
		curr_hours *= 7
		curr_hours += lst[j]
		j += 1
	curr_minutes = lst[yach_n]
	j = yach_n + 1
	while j < yach:
		curr_minutes *= 7
		curr_minutes += lst[j]
		j += 1
	if curr_hours < n and curr_minutes < m:
		amount += 1
	if curr_hours >= n:
		break
print(amount)


import math, itertools
(n, m) = map(int, input().split())
n -= 1
m -= 1
if n > 0:
	a = math.floor(math.log(n, 7)) + 1
else:
	a = 1
if m > 0:
	b = math.floor(math.log(m, 7)) + 1
else:
	b = 1
c = 0
if a > 7 or b > 7:
	print(0)
	quit()
x = list(itertools.permutations(range(0, 7), a + b))
for p in x:
	s = 0
	t = 0
	for i in range(a):
		s += p[i] * 7 ** (a - 1 - i)
	for i in range(a, a + b):
		t += p[i] * 7 ** (a + b - 1 - i)
	if s <= n and t <= m:
		c += 1
print(c)


from itertools import permutations
import math, sys
inp = input().split()
n = int(inp[0]) - 1
m = int(inp[1]) - 1
na = []
ma = []
ln = 0
lm = 0
while n > 0:
	na.append(n % 7)
	n //= 7
	ln += 1
while m > 0:
	ma.append(m % 7)
	m //= 7
	lm += 1
na.reverse()
ma.reverse()
cnt = ln + lm
if cnt > 7 or cnt < 1:
	print(0)
	sys.exit()
if ln == 0:
	na.append(0)
if lm == 0:
	ma.append(0)
ln = max(1, ln)
lm = max(1, lm)
perm = permutations([x for x in range(0, 7)], ln + lm)
ex = 0
ex2 = 0
ans = 0
t1 = 0
t2 = 0
for i in perm:
	ex = 2
	ex2 = 2
	s = ''
	for k in range(0, ln):
		t1 = int(na[k])
		t2 = int(i[k])
		if t1 > t2:
			ex = 1
			break
		if t1 < t2:
			ex = 0
			break
	if ex == 2 and t1 == t2:
		ex = 1
	for k in range(0, lm):
		t1 = int(ma[k])
		t2 = int(i[ln + k])
		if t1 > t2:
			ex2 = 1
			break
		if t1 < t2:
			ex = 0
			break
	if ex2 == 2 and t1 == t2:
		ex2 = 1
	if ex == 1 and ex2 == 1:
		ans += 1
print(ans)


from itertools import permutations

def C(l):
	for i in range(15):
		if 7 ** i > l:
			return max(i, 1)
	return -1
(n, m) = map(int, input().split())
n -= 1
m -= 1
(n1, n2) = (C(n), C(m))
if n1 + n2 > 7:
	print(0)
	exit()
ans = 0
for i in list(permutations(list(range(0, 7)), n1 + n2)):
	(ans1, ans2) = (0, 0)
	for j in range(n1):
		ans1 += 7 ** j * i[j]
	for j in range(n1, n1 + n2):
		ans2 += 7 ** (j - n1) * i[j]
	if ans1 <= n and ans2 <= m:
		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) = map(int, input().split())
print(solve(n, m))


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
base = 7
digits = [x for x in range(base)]

def get_count(n):
	answer = 1
	while n >= base:
		n = n // base
		answer += 1
	return answer

def is_good(tup, n):
	a = 0
	for x in reversed(tup):
		a *= base
		a += x
	if a >= n:
		return False
	else:
		return True
(n, m) = map(int, input().split())
h_count = get_count(n - 1)
m_count = get_count(m - 1)
answer = 0
for perm in permutations(digits, h_count + m_count):
	if is_good(perm[:h_count], n) and is_good(perm[h_count:], m):
		answer += 1
print(answer)


from math import log
(n, m) = tuple(map(int, input().split()))

def places(a):
	p = 7
	c = 1
	while p < a:
		p *= 7
		c += 1
	return c
dn = places(n)
dm = places(m)
ans = []

def valid_time(s):
	return int(s[:dn], 7) < n and int(s[dn:], 7) < m

def gen(s):
	if len(s) == dn + dm:
		if valid_time(s):
			global ans
			ans.append(s)
	for c in '0123456':
		if c not in s:
			gen(s + c)
if dn + dm > 7:
	print('0')
else:
	gen('')
	print(len(ans))


def get_size(x):
	val = 7
	size = 1
	while True:
		if val - 1 >= x - 1:
			return size
		val *= 7
		size += 1

def get_num(L):
	res = 0
	for x in L:
		res = res * 7 + x
	return res
n = None
m = None
n_size = None
m_size = None
vis = None
record = None

def dfs(floor):
	if floor == n_size + m_size:
		return 0 if get_num(record[n_size:]) >= m else 1
	if floor == n_size:
		if get_num(record[:n_size]) >= n:
			return 0
	res = 0
	for i in range(7):
		if vis[i]:
			vis[i] = False
			record[floor] = i
			res += dfs(floor + 1)
			vis[i] = True
	return res

def main():
	global n, m, n_size, m_size
	(n, m) = map(int, input().split())
	n_size = get_size(n)
	m_size = get_size(m)
	if n_size + m_size > 7:
		return 0
	global vis, record
	vis = [True] * 7
	record = [None] * (n_size + m_size)
	return dfs(0)
print(main())


import sys
(h, m) = [int(x) for x in input().split()]

def convert(dec, min_len):
	sev = []
	if dec == 0:
		if min_len > 0:
			return [0] * min_len
		return [0]
	while dec:
		sev.append(int(dec % 7))
		dec = int(dec / 7)
	ret = sev[::-1]
	if min_len > 0:
		return [0] * (min_len - len(ret)) + ret
	return ret
h_list = convert(h - 1, 0)
m_list = convert(m - 1, 0)
h_len = len(h_list)
m_len = len(m_list)
if h_len + m_len > 7:
	print(0)
	sys.exit(0)
th = 0
tm = 0

def check(num, min_len, have):
	n_digits = 0
	while True:
		digit = int(num % 7)
		n_digits += 1
		if have[digit]:
			return False
		have[digit] = True
		num = int(num / 7)
		if num == 0:
			break
	if min_len - n_digits > 1:
		return False
	if min_len - n_digits == 1:
		if have[0]:
			return False
		have[0] = True
	return True
cnt = 0
h_have = [False] * 7
while th < h and (not check(th, h_len, h_have)):
	th += 1
	h_have = [False] * 7
while th < h:
	if check(tm, m_len, h_have[:]):
		cnt += 1
	tm += 1
	if tm == m:
		th += 1
		h_have = [False] * 7
		while th < h and (not check(th, h_len, h_have)):
			th += 1
			h_have = [False] * 7
		tm = 0
print(cnt)


from itertools import permutations
(n, m) = map(int, input().split())

def S(x):
	if x == 0:
		return 1
	res = 0
	while x > 0:
		x //= 7
		res += 1
	return res
(a, b) = (S(n - 1), S(m - 1))
if a + b > 7:
	print(0)
	exit()
D = list(range(7))
res = 0
for v in permutations(D, a):
	x = 0
	for i in range(a):
		x = 7 * x + v[i]
	if x >= n:
		continue
	R = [d for d in D if d not in v]
	for u in permutations(R, b):
		y = 0
		for i in range(b):
			y = 7 * y + u[i]
		res += y < m
print(res)


from itertools import permutations

def numberToBase(n):
	if n == 0:
		return []
	return numberToBase(n // 7) + [n % 7]
(n, m) = map(int, input().split())
(x, y) = (numberToBase(n - 1), numberToBase(m - 1))
(nl, ml) = (max(1, len(x)), max(1, len(y)))
r = 0
for p in permutations('0123456', nl + ml):
	(a, b) = (''.join(p[:nl]), ''.join(p[nl:]))
	r += int(a, 7) < n and int(b, 7) < m
print(r)


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)


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

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 time
import math

def to7(x):
	ans = 0
	k = 1
	i = 0
	while x > 0:
		d = divmod(x, 7)
		ans = ans + d[1] * k
		k = k * 10
		x = d[0]
		i = i + 1
	if i == 0:
		i = 1
	return (ans, i)

def func(step, nnow, mnow, n, m, ln, lm, fig):
	if step == ln:
		return 1
	k = 10 ** (ln - step - 1)
	ans = 0
	for i in range(7):
		if fig[i] == False:
			if nnow + k * i <= n:
				fig[i] = True
				if ln - lm <= step:
					for j in range(7):
						if fig[j] == False:
							if mnow + k * j <= m:
								fig[j] = True
								ans = ans + func(step + 1, nnow + k * i, mnow + k * j, n, m, ln, lm, fig)
								fig[j] = False
				else:
					ans = ans + func(step + 1, nnow + k * i, mnow, n, m, ln, lm, fig)
				fig[i] = False
	return ans
(n, m) = (int(i) - 1 for i in input().split())
start = time.time()
fig = [False for i in range(7)]
if m > n:
	(n, m) = (m, n)
(n, ln) = to7(n)
(m, lm) = to7(m)
if ln + lm > 7:
	ans = 0
else:
	ans = func(0, 0, 0, n, m, ln, lm, fig)
print(ans)
finish = time.time()


(n, m) = map(int, input().split())
p1 = 0
while 7 ** p1 < n:
	p1 += 1
p2 = 0
while 7 ** p2 < m:
	p2 += 1
cnt = 0
(p1, p2) = (max(p1, 1), max(p2, 1))
from itertools import permutations
for perm in permutations(list(range(0, 7)), p1 + p2):
	a = b = 0
	for k in range(p1):
		a += perm[k] * 7 ** k
	for k in range(p1, p1 + p2):
		b += perm[k] * 7 ** (k - p1)
	if a < n and b < m and perm:
		cnt += 1
print(cnt)


from itertools import permutations
(n, m) = map(int, input().split())

def l(x):
	r = x == 0
	while x:
		r += 1
		x //= 7
	return r
(ans, ln, lm) = (0, l(n - 1), l(m - 1))
for s in permutations('0123456', ln + lm):
	s = ''.join(s)
	ans += int(s[:ln], 7) < n and int(s[ln:], 7) < m
print(ans)


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

def GetDigitsNumberIn7System(number):
	result = 1
	x = 7
	while True:
		if number <= x - 1:
			return result
		else:
			result += 1
			x *= 7

def fillZeros(s, razryadCount):
	for i in range(razryadCount - len(s)):
		s = '0' + s
	return s

def HasTheSameDigits(s):
	digits = set()
	for letter in s:
		digits.add(letter)
	if len(digits) == len(s):
		return False
	else:
		return True

def int2base(x, base):
	if x < 0:
		sign = -1
	elif x == 0:
		return '0'
	else:
		sign = 1
	x *= sign
	res = ''
	while x != 0:
		res += str(x % base)
		x //= base
	return res[::-1]

def GetNumberOfVariations(hoursInDay, minutesInHour):
	n1 = GetDigitsNumberIn7System(hoursInDay - 1)
	n2 = GetDigitsNumberIn7System(minutesInHour - 1)
	count = 0
	listHours = []
	listMinutes = []
	for i in range(hoursInDay):
		if HasTheSameDigits(fillZeros(int2base(i, 7), n1)) == False:
			listHours.append(i)
	for i in range(minutesInHour):
		if HasTheSameDigits(fillZeros(int2base(i, 7), n2)) == False:
			listMinutes.append(i)
	for i in listHours:
		for j in listMinutes:
			str1 = fillZeros(int2base(i, 7), n1)
			str2 = fillZeros(int2base(j, 7), n2)
			strc = str1 + str2
			if HasTheSameDigits(strc) == False:
				count += 1
	return count
r1 = GetDigitsNumberIn7System(n - 1)
r2 = GetDigitsNumberIn7System(m - 1)
if r1 + r2 > 7:
	print(0)
else:
	res = GetNumberOfVariations(n, m)
	print(res)


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

def go(on1, val1, on2, val2, has):
	res = 0
	if on1 > 0:
		for i in range(7):
			if i not in has:
				if val1 * 7 + i < n:
					res += go(on1 // 7, val1 * 7 + i, on2, val2, has + [i])
	elif on2 > 0:
		for i in range(7):
			if i not in has:
				if val2 * 7 + i < m:
					res += go(on1, val1, on2 // 7, val2 * 7 + i, has + [i])
	else:
		res += 1
	return res
print(go(max(1, n - 1), 0, max(1, m - 1), 0, []))


from math import *
d = False
(n, m) = list(map(int, input().split()))
if n >= 7 ** 7 or m >= 7 ** 7:
	print(0)
	exit()

def digs(num):
	return 1 if num < 5 else ceil(log(num, 7))
dig_n = digs(n)
dig_m = digs(m)
if d:
	print(dig_n, dig_m)

def f(hour, dn, minutes, dm, l):
	if dn == 0 and dm == 0:
		res = 1
		if d:
			print(hour, minutes)
	else:
		res = 0
	for i in l:
		lp = [x for x in l if x != i]
		if d:
			print(lp)
		h = hour * 7 + i
		mi = minutes * 7 + i
		if h < n and dn != 0:
			res += f(h, dn - 1, minutes, dm, lp)
		elif dn == 0 and mi < m and (dm != 0):
			res += f(hour, dn, mi, dm - 1, lp)
	return res
print(f(0, dig_n, 0, dig_m, range(7)))


BASE = 7

class Solution(object):

	def __init__(self, m, n):
		self.max_hour = self.itov(n)
		self.max_min = self.itov(m)
		self.used = used = [False] * BASE

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

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

def main():
	(n, m) = map(int, input().split())
	n -= 1
	m -= 1
	s = Solution(m, n)
	print(s.gen())
main()


from math import factorial as f

def b8(n):
	if n == 0:
		return '0'
	nums = []
	while n:
		(n, r) = divmod(n, 7)
		nums.append(str(r))
	return ''.join(reversed(nums))
(n, m) = map(int, input().split())
n8 = b8(n - 1)
m8 = b8(m - 1)

def isvalid(t, n8, m8):
	if len(t) <= len(n8):
		return t <= n8[:len(t)]
	else:
		return t[len(n8):] <= m8

def bf(t, n8, m8, i, unused):
	if i == len(n8 + m8):
		c = 1
	else:
		c = 0
		for ui in unused:
			tt = t + ui
			if isvalid(tt, n8, m8):
				c += bf(tt, n8, m8, i + 1, unused - {ui})
	return c
if len(n8 + m8) > 7:
	sol = 0
else:
	sol = bf('', n8, m8, 0, set([str(i) for i in range(7)]))
print(sol)


(n, m) = map(int, input().split())
p1 = 1
while 7 ** p1 < n:
	p1 += 1
p2 = 1
while 7 ** p2 < m:
	p2 += 1
cnt = 0
from itertools import permutations
for perm in permutations(list('0123456'), p1 + p2):
	a = int(''.join(perm[:p1]), 7)
	b = int(''.join(perm[p1:p1 + p2]), 7)
	if a < n and b < m:
		cnt += 1
print(cnt)


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())


import itertools

def tobase(n):
	if n == 0:
		return 1
	l = 0
	while n > 0:
		l += 1
		n //= 7
	return l

def main():
	(n, m) = map(int, input().split())
	nlen = tobase(n - 1)
	mlen = tobase(m - 1)
	places = nlen + mlen
	ans = 0
	for perm in itertools.permutations('0123456', places):
		hour = ''.join(perm[:nlen])
		minute = ''.join(perm[nlen:])
		if int(hour, 7) < n and int(minute, 7) < m:
			ans += 1
	print(ans)
main()


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

def toBase7D(x):
	base = 1
	digits = 1
	while base * 7 - 1 < x:
		base *= 7
		digits += 1
	return digits

def toBase7(x):
	base = 1
	number = ''
	while base * 7 - 1 < x:
		base *= 7
	while base != 0:
		d = math.floor(x / base)
		number += str(d)
		x -= d * base
		base = math.floor(base / 7)
	return number

def checkDigits(a, b):
	digits = []
	for d in a:
		if d in digits:
			return False
		else:
			digits.append(d)
	for d in b:
		if d in digits:
			return False
		else:
			digits.append(d)
	return True

def substract(x, digits):
	xx = int(x)
	xx -= 1
	if xx < 0:
		return False
	x = str(xx)
	newX = ''
	for d in x:
		if d == '7' or d == '8' or d == '9':
			newX += '6'
		else:
			newX += d
	if digits - len(newX) >= 2:
		return False
	elif digits - len(newX) == 1:
		newX = '0' + newX
	return newX

def check(n, nDigits, m, mDigits):
	nn = toBase7(n - 1)
	mm2 = toBase7(m - 1)
	total = 0
	while True:
		mm = mm2
		if checkDigits(nn, []):
			while True:
				if checkDigits(nn, mm):
					total += 1
				mm = substract(mm, mDigits)
				if not mm:
					break
		nn = substract(nn, nDigits)
		if not nn:
			break
	print(total)
nDigits = toBase7D(n - 1)
mDigits = toBase7D(m - 1)
if nDigits + mDigits > 7:
	print(0)
	exit()
elif nDigits < mDigits:
	check(m, mDigits, n, nDigits)
else:
	check(n, nDigits, m, mDigits)


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)


read = lambda : map(int, input().split())
(n, m) = read()
from itertools import permutations, combinations
cnt = 0
p1 = 0
x1 = 1
while x1 <= n - 1:
	x1 *= 7
	p1 += 1
p2 = 0
x2 = 1
while x2 <= m - 1:
	x2 *= 7
	p2 += 1
(p1, p2) = (max(p1, 1), max(p2, 1))
Len = p1 + p2
for comb in combinations([0, 1, 2, 3, 4, 5, 6], Len):
	for perm in permutations(comb):
		a = b = 0
		p = 1
		for k in range(p1 - 1, -1, -1):
			a += perm[k] * p
			p *= 7
		p = 1
		for k in range(Len - 1, p1 - 1, -1):
			b += perm[k] * p
			p *= 7
		if a >= n or b >= m:
			continue
		if perm:
			cnt += 1
print(cnt)