We define super digit of an integer $\boldsymbol{x}$ using the following rules:  

Given an integer, we need to find the super digit of the integer.

If $\boldsymbol{x}$ has only $\mbox{I}$ digit, then its super digit is $\boldsymbol{x}$.   
Otherwise, the super digit of $\boldsymbol{x}$ is equal to the super digit of the sum of the digits of $\boldsymbol{x}$.  

For example, the super digit of $9875$ will be calculated as:

	super_digit(9875)   	9+8+7+5 = 29 
	super_digit(29) 	2 + 9 = 11
	super_digit(11)		1 + 1 = 2
	super_digit(2)		= 2  

Example 

$n='\:9875'$ 

$k=4$  

The number $\boldsymbol{p}$ is created by concatenating the string $n$ $\boldsymbol{\mbox{k}}$ times so the initial $p=9875987598759875$.

    superDigit(p) = superDigit(9875987598759875)
                  9+8+7+5+9+8+7+5+9+8+7+5+9+8+7+5 = 116
    superDigit(p) = superDigit(116)
                  1+1+6 = 8
    superDigit(p) = superDigit(8)

All of the digits of $\boldsymbol{p}$ sum to $\textbf{116}$.  The digits of $\textbf{116}$ sum to $8$.  $8$ is only one digit, so it is the super digit.

Function Description

Complete the function superDigit in the editor below.  It must return the calculated super digit as an integer.  

superDigit has the following parameter(s):  

string n: a string representation of an integer  
int k: the times to concatenate $n$ to make $\boldsymbol{p}$   

Returns  

int: the super digit of $n$ repeated $\boldsymbol{\mbox{k}}$ times  

Input Format

The first line contains two space separated integers, $n$ and $\boldsymbol{\mbox{k}}$.  

Constraints

$1\leq n<10^{100000}$
$1\leq k\leq10^5$

Sample Input 0
148 3

Sample Output 0
3

Explanation 0

Here  $n=148$ and $k=3$, so $p=148148148$. 

super_digit(P) = super_digit(148148148) 
               = super_digit(1+4+8+1+4+8+1+4+8)
               = super_digit(39)
               = super_digit(3+9)
               = super_digit(12)
               = super_digit(1+2)
               = super_digit(3)
               = 3

Sample Input 1
9875 4

Sample Output 1
8

Sample Input 2
123 3

Sample Output 2
9

Explanation 2

Here  $n=123$ and $k=3$, so $p=123123123$. 

super_digit(P) = super_digit(123123123) 
               = super_digit(1+2+3+1+2+3+1+2+3)
               = super_digit(18)
               = super_digit(1+8)
               = super_digit(9)
               = 9

Source: hackerrank | Difficulty: MEDIUM
Tags: Complete search


Solutions:
def digits_sum(n):
	s = 0
	while n > 9:
		s += n % 10
		n //= 10
	s += n
	return s

def super_digit(n):
	d = digits_sum(n)
	if d < 10:
		return d
	return super_digit(d)

def super_digit_iter(n):
	d = digits_sum(n)
	while d > 9:
		d = digits_sum(d)
	return d

def main():
	(n, k) = map(int, input().strip().split(' '))
	print(super_digit_iter(digits_sum(n) * k))
main()


def sup_digits(x, k):
	a = digsum(x)
	return sup_digit(str(int(a) * k))

def sup_digit(x):
	if len(x) <= 1:
		return x
	else:
		return sup_digit(digsum(x))

def digsum(x):
	return str(sum((int(i) for i in list(x))))
(n, k) = input().split()
print(sup_digits(n, int(k)))


a = list(map(str, input().split()))

def super_digit(n):
	sum = 0
	if len(n) == 1:
		print(n)
	else:
		a = len(n)
		for i in range(a):
			sum = sum + int(n[i])
		super_digit(str(sum))
sum = 0
a1 = len(a[0])
for i in range(a1):
	sum = sum + int(a[0][i])
n = sum * int(a[1])
c = super_digit(str(n))


def super_digit(x):
	if x // 10 == 0:
		return x
	else:
		l = [int(i) for i in str(x)]
		dsum = sum(l)
		return super_digit(dsum)
(n, kk) = input().strip().split(' ')
k = int(kk)
p = 0
for d in n:
	p += int(d) * k
print(super_digit(p))


def get_sum(string):
	total = 0
	for s in string:
		total += int(s)
	return total
data = input().strip().split(' ')
n = get_sum(data[0]) * int(data[1])
while n > 10:
	n = get_sum(str(n))
print(n)


line = input().strip().split(' ')
k = int(line[1])
p = int(line[0])
digits = [int(x) for x in str(p)]
p = sum(digits) * k

def super_digit(num):
	if len(str(num)) == 1:
		return num
	else:
		digits = [int(x) for x in str(num)]
		return super_digit(sum(digits))
print(super_digit(p))


(a, b) = input().split(' ')
y = a
z = 0
for i in range(len(y)):
	z = z + int(y[i])
y = str(z)
c = int(y) * int(b)
y = str(c)
while len(y) != 1:
	z = 0
	for i in range(len(y)):
		z = z + int(y[i])
	y = str(z)
print(y)


import sys

def orig_sd(n, k):
	sum = 0
	for i in list(str(n)):
		sum += int(i)
	return super_digit(sum * k)

def super_digit(n):
	l = list(str(n))
	if len(l) <= 1:
		return int(l[0])
	sum = 0
	for i in l:
		sum += int(i)
	return super_digit(sum)
for l in sys.stdin:
	l2 = l.split(' ')
	print(orig_sd(int(l2[0]), int(l2[1])))


def super_digit(n):
	string = str(n)
	if len(string) == 1:
		return n
	sum = 0
	length = len(string)
	for i in range(length):
		sum += int(string[i])
	return super_digit(sum)
(n, k) = input().strip().split(' ')
k = int(k)
sum = 0
for i in range(len(n)):
	sum += int(n[i])
sum *= k
print(super_digit(sum))


x = input().split()
(n, k) = (x[0], x[1])

def digit_sum(a):
	sum = 0
	for dig in a:
		sum += int(dig)
	return str(sum)

def super_digit(n):
	if len(n) == 1:
		return n
	else:
		while len(n) != 1:
			n = digit_sum(n)
	return n

def super_digi(n):
	if len(n) == 1:
		return n
	else:
		return super_digit(digit_sum(n))
print(super_digit(super_digit(str(n)) * int(k)))


def s(d):
	n = d
	while len(n) > 1:
		c = 0
		for i in range(len(n)):
			c += int(n[i:i + 1])
		n = str(c)
	return n
(n, k) = input().split()
n = s(n)
n = n * int(k)
print(s(n))


def sum_digits(n):
	r = 0
	while n:
		(r, n) = (r + n % 10, n // 10)
	return r

def super_digit(x):
	if x < 10:
		return x
	return super_digit(sum_digits(x))
(n, k) = input().split(' ')
k = int(k)
if len(n) == 1 and k == 1:
	print(n)
else:
	print(super_digit(sum((int(c) * k for c in n))))


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

def super_digit(p):
	if len(p) == 1:
		return int(p)
	else:
		tot = 0
		for i in p:
			tot += int(i)
		return super_digit(str(tot))
if int(n) < 10 and int(n) * k < 10:
	print(int(n * k))
else:
	tot = 0
	for i in n:
		tot += int(i)
	tot = tot * k
	print(super_digit(str(tot)))


import sys
(num, k) = input().strip().split(' ')
flag = 1
while True:
	sum1 = 0
	for i in range(0, len(num)):
		sum1 = int(sum1) + int(num[i])
	if sum1 < 10:
		if flag == 0:
			print(sum1)
			sys.exit()
		num = sum1 * int(k)
		if num < 10:
			print(num)
			break
		else:
			num = str(num)
			flag = 0
	else:
		num = str(sum1)


(n, k) = input().split()
k = int(k)
n = sum((int(v) for v in n)) * k
while n > 9:
	n = sum((int(v) for v in str(n)))
print(n)


def sd(n):
	s = 0
	while n:
		(n, r) = divmod(n, 10)
		s += r
	return s
(n, k) = map(int, input().split())
n = k * sd(n)
while n > 9:
	n = sd(n)
print(n)


def sumOfDigits(num):
	num = str(num)
	length = len(num)
	sum = 0
	for i in range(length):
		sum += int(num[i])
	return sum

def superDigit(num):
	if len(str(num)) == 1:
		return num
	return superDigit(sumOfDigits(num))
temp = input().split()
n = int(temp[0])
k = int(temp[1])
num = sumOfDigits(n) * k
if len(str(num)) == 1:
	print(num)
else:
	answer = superDigit(num)
	print(answer)


(string, k) = input().split(' ')
(string, k) = (int(string), int(k))
number = 0
while string != 0:
	number += string % 10
	string //= 10
(string, number) = (number * k, 0)
while string // 10 != 0:
	while string != 0:
		number += string % 10
		string //= 10
	(string, number) = (number, 0)
print(string)


def sup(d):
	c = str(d)
	if len(c) == 1:
		return d
	s = [int(temp) for temp in c]
	return sup(sum(s))
(n, k) = [int(temp) for temp in input().strip().split(' ')]
print(sup(sup(n) * k))


def digitsum(m):
	if m <= 9:
		return m
	else:
		return digitsum(sum([int(c) for c in str(m)]))

def solve(n, k):
	return digitsum(digitsum(n) * k)
(n, k) = map(int, input().split())
print(solve(n, k))


(n, k) = [int(i) for i in input().split()]
ans = n % 9
ans = ans * k
ans = ans % 9
if ans == 0:
	print(9)
else:
	print(ans)


def getDigitSum(s):
	while len(s) != 1:
		sum = 0
		for i in s:
			sum += int(i)
		s = str(sum)
	return int(s)
(s, k) = input().split(' ')
s = getDigitSum(s)
s *= k
print(getDigitSum(s))


def super_digit(n):
	sum = 0
	if len(n) == 1:
		print(n)
	else:
		a = len(n)
		for i in range(a):
			sum = sum + int(n[i])
		super_digit(str(sum))
a = list(map(str, input().split()))
sum = 0
b = len(a[0])
for i in range(b):
	sum = sum + int(a[0][i])
n = sum * int(a[1])
c = super_digit(str(n))


def super_digit(n):
	if n < 10:
		return n
	else:
		return super_digit(sum(map(int, list(str(n)))))
(n, k) = map(int, input().strip().split())
print(super_digit(sum(map(int, list(str(n)))) * k))


def digSum(num):
	return sum(map(int, str(num)))

def super_digit(n, k):
	initList = [digSum(n) * k]
	return helper(initList)

def helper(listDigs):
	if len(listDigs) == 1 and listDigs[0] < 10:
		return listDigs[0]
	else:
		sumDigs = sum(map(lambda x: digSum(x), listDigs))
		return helper(list(map(int, str(sumDigs))))
inputValueList = input().split(' ')
print(super_digit(int(inputValueList[0]), int(inputValueList[1])))


import sys

def superdigit(n):
	if len(n) <= 1:
		return n
	else:
		sum_ = 0
		for d in n:
			sum_ += int(d)
		s = superdigit(str(sum_))
	return s
(n, k) = map(int, input().split(' '))
res = superdigit(str(n * k))
print(res)


import sys

def find_ans(n):
	if n < 10:
		return n
	s = 0
	while n > 0:
		s += n % 10
		n //= 10
	return find_ans(s)

def main():
	(n, k) = input().split(' ')
	k = int(k)
	s = 0
	for i in n:
		s += int(i)
	s *= k
	print(find_ans(s))
	sys.exit(0)
main()


def super_digit(n):
	if n < 10:
		return n
	else:
		return super_digit(sum(map(int, list(str(n)))))
(n, k) = map(int, input().strip().split())
print(super_digit(sum(map(int, list(str(n)))) * k))


def super_digit(s, k):
	n2 = sds(s) * k
	while n2 >= 10:
		n2 = sd(n2)
	return n2

def sds(s):
	acc = 0
	while s:
		acc += int(s.pop())
	return acc

def sd(n):
	acc = 0
	while n > 0:
		m = n % 10
		acc += m
		n //= 10
	return acc
(ns, ks) = input().strip().split()
k = int(ks)
print(super_digit(list(ns), k))


def f1(x):
	a = list(str(x))
	return int(a[0]) + int(a[1])
(n, k) = input().strip().split()
a = 0
for i in n:
	a += int(i)
	if a > 9:
		a = f1(a)
n = str(a * k)
a = 0
for i in n:
	a += int(i)
	if a > 9:
		a = f1(a)
print(a)


(n, k) = input().split(' ')
k = int(k)

def super_digit(nstr):
	if len(nstr) > 1:
		return super_digit(str(sum([int(x) for x in nstr])))
	else:
		return nstr
print(super_digit(str(int(super_digit(n)) * k)))


def solve(n, k):
	if n < 10:
		return n
	s = sum((int(a) for a in str(n)))
	s *= k
	return solve(s, 1)
(n, k) = (int(a) for a in input().split())
print(solve(n, k))


import sys

def dig_sum(val_string):
	sum_value = 0
	for digit in val_string:
		sum_value += int(digit)
		sum_value %= 9
	return sum_value
[n, k] = input().split()
number1 = sum(map(int, list(str(n)))) % 9
number2 = sum(map(int, list(str(k)))) % 9
val = number1 * number2 % 9
if val == 0:
	print(9)
else:
	print(val)


def super_digit(num, k):
	if len(num) == 1 and k == 1:
		return num
	else:
		num = str(sum(map(int, list(num))) * k)
		return super_digit(num, 1)
(n, k) = input().split()
k = int(k)
print(super_digit(n, k))


def bigDigit(num):
	if len(num) == 1:
		return num
	su = 0
	for a in num:
		su += int(a)
	return bigDigit(str(su))
(a, k) = input().split(' ')
print(bigDigit(str(bigDigit(a)) * int(k)))


def sumOfDigits(num, k):
	if num < 10 and k == 0:
		return num
	if num < 10 and k != 0:
		num = str(num) * k
		k = 0
	sum = 0
	for i in str(num):
		sum = sum + int(i)
	return sumOfDigits(sum, k)
(x, z) = input().split(' ')
(x, z) = (int(x), int(z))
print(sumOfDigits(x, z))


import sys

def findout(N):
	if N < 10:
		return N
	else:
		A = str(N)
		temp = 0
		for i in range(0, len(A)):
			temp = temp + int(A[i])
		return findout(temp)
A = list(map(int, input().strip().split(' ')))
n = A[0]
k = A[1]
string = str(n)
num = 0
for i in range(len(string)):
	num = num + int(string[i])
print(findout(num * k))


import sys

def get_digits(n):
	return [int(i) for i in list(str(n))]

def get_digit_sum(n):
	return sum(get_digits(n))

def get_tuple_from_stdin():
	l = sys.stdin.readline().strip()
	return [int(i) for i in l.split(' ')]
(n, k) = get_tuple_from_stdin()
cur_n = get_digit_sum(n) * k
while True:
	next_n = get_digit_sum(cur_n)
	if cur_n == next_n:
		break
	cur_n = next_n
print(cur_n)


lis1 = [i for i in input().split(' ')]
n = lis1[0]
k = int(lis1[1])
p = int
s = str

def recurse(n, count=0):
	if len(n) == 1:
		return n
	sum1 = 0
	for i in n:
		sum1 += p(i)
	sum1 = s(sum1)
	if count == 0:
		sum1 *= k
		count = 1
	y = recurse(sum1, count)
	return y
print(recurse(n))


def super_digit(num):
	if num < 10:
		return num
	s = str(num)
	sum = 0
	for i in s:
		sum += int(i)
	return super_digit(sum)
(n, k) = input().split()
k = int(k)
sum = 0
for i in n:
	sum += int(i)
print(super_digit(sum * k))


(n, k) = [int(x) for x in input().split()]
sums = 0
while n > 0:
	r = n % 10
	sums = sums + r
	n = n // 10
n = sums * k
m = n
while n > 0:
	sums = 0
	while m > 0:
		r = m % 10
		sums = sums + r
		m = m // 10
	n = sums
	m = sums
	n = n // 10
print(sums)


(n, k) = list(map(int, input().split()))
s = 0
while n:
	s = (s + n % 10) % 9
	n //= 10
m = s * k % 9
print([m, 9][m == 0])


def super_digit(n):
	while len(str(n)) != 1:
		n = sum(map(int, list(str(n))))
	return n
(n, k) = [int(x) for x in input().split(' ')]
N = k * sum(map(int, list(str(n))))
print(super_digit(N))


hop = input()
arr = hop.split()
n_str = arr[0]
k = int(arr[1])
n_list = []
for char in n_str:
	n_list.append(int(char))
n_sum = sum(n_list)
N = n_sum * k

def digitSum(x):
	x_str = str(x)
	if len(x_str) == 1:
		return x
	else:
		x_list = []
		for char in x_str:
			x_list.append(int(char))
		x_sum = sum(x_list)
		return digitSum(x_sum)
print(digitSum(N))


def sol(N):
	N = sum((int(i) for i in str(N)))
	if N // 10 == 0:
		return N
	else:
		return sol(N)
(n, k) = input().split()
k = int(k)
N = sum((int(i) for i in n)) * k
print(sol(N))


def super_digit(p):
	digits = list(map(int, str(p)))
	if len(digits) == 1:
		return digits[0]
	else:
		return super_digit(sum(digits))

def super_digit_n_k(n, k):
	return super_digit(super_digit(n) * k)
(n, k) = map(int, input().split())
print(super_digit_n_k(n, k))


def super_digit(n):
	if len(str(n)) == 1:
		return n
	else:
		n = sum([int(i) for i in str(n)])
		return super_digit(n)
(n, k) = [int(i) for i in input().split()]
n = sum([int(i) for i in str(n)])
n = n * k
i = super_digit(n)
print(i)


def super_digit(num):
	if num // 10 == 0:
		return num
	else:
		return super_digit(sum(list(map(int, list(str(num))))))
(n, p) = map(int, input().strip().split(' '))
val = super_digit(sum(list(map(int, list(str(n))))) * p)
print(val)


def super_digit(string, k):
	sum = 0
	for digit in list(string):
		sum += int(digit)
	sum *= k
	rslt = 9 if sum % 9 == 0 else sum % 9
	return rslt
(string, k) = [value for value in input().strip().split(' ')]
k = int(k)
print(super_digit(string, k))


def super_digit(string):
	if len(string) == 1:
		return int(string)
	sum = 0
	for digit in list(string):
		sum += int(digit)
	return super_digit(str(sum))

def super_digit2(string, k):
	sum = 0
	for digit in list(string):
		sum += int(digit)
	sum *= k
	rslt = 9 if sum % 9 == 0 else sum % 9
	return rslt
(string, k) = [value for value in input().strip().split(' ')]
k = int(k)
print(super_digit2(string, k))


import sys

def powerdigit(n):
	if n < 10:
		return n
	else:
		p = 0
		while n > 0:
			p += n % 10
			n //= 10
		return powerdigit(p)

def digitsum(n):
	d = 0
	while n > 0:
		d += n % 10
		n //= 10
	return d
(n, k) = (int(x) for x in input().strip().split())
print(powerdigit(digitsum(n) * k))


(n, k) = input().strip().split(' ')

def digsum(n):
	if len(n) == 1:
		return int(n)
	s = 0
	for i in range(0, len(n)):
		s += int(n[i])
	return digsum(str(s))
print(digsum(str(digsum(n) * k)))


def supersum2(n, dSum):
	cache = {}
	while len(str(dSum)) != 1:
		if dSum in cache:
			dSum = cache[dSum]
		else:
			val = sum((int(x) for x in str(dSum)))
			cache[dSum] = val
			dSum = val
	return dSum
(n, k) = input().split()
k = int(k)
dSum = sum((int(x) for x in n))
init_sum = supersum2(n, dSum)
dSum = sum((int(x) for x in str(init_sum * k)))
print(supersum2(str(init_sum * k), dSum))


def super_digit(n):
	if n <= 9:
		return n
	return super_digit(sum((int(d) for d in str(n))))
(n, k) = input().strip().split(' ')
sum_digits = sum((int(d) for d in n))
print(super_digit(sum_digits * int(k)))


def quersumme(inputNumber):
	return sum([int(n) for n in list(inputNumber)])
(n, k) = input().strip().split(' ')
k = int(k)
qs = quersumme(n) * k
while qs > 9:
	qs = quersumme(str(qs))
print(qs)


import sys
sys.setrecursionlimit(10000)

def superdigit(n, k=1):
	if len(n) == 1:
		return n
	dsum = sum((int(c) for c in n)) * k
	return superdigit(str(dsum))
(n, k) = input().split()
k = int(k)
print(superdigit(n, k))


def func(a):
	if a < 10:
		return a
	temp = 0
	a = str(a)
	for i in range(len(a)):
		temp += int(a[i])
	return func(temp)
(n, k) = input().split()
l = func(int(n))
print(func(l * int(k)))


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

def super_digit(a):
	if a < 10:
		return a
	return super_digit(sumdig(a))

def sumdig(a):
	a = str(a)
	s = 0
	for i in range(len(a)):
		s += int(a[i])
	return s
print(super_digit(super_digit(int(n)) * k))


__author__ = 'sidwagz'
(N, K) = input().strip().split(' ')

def sum_of_digits(N):
	return sum([int(_) for _ in N])
t = str(sum_of_digits(N))
t = t * int(K)
while len(t) > 1:
	t = str(sum_of_digits(t))
print(t)


def superdigit(n):
	if n < 10:
		return n
	else:
		x = sum([ord(c) - ord('0') for c in '{}'.format(n)])
		return superdigit(x)
(n, k) = [int(i) for i in input().split(' ')]
print(superdigit(k * n))


def super_digit(n, k):
	if n // 10 == 0:
		return n
	nums = []
	while n != 0:
		nums.append(n % 10)
		n //= 10
	return super_digit(sum(nums) * k, 1)
(n, k) = list(map(int, input().split()))
print(super_digit(n, k))


def count_back(N):
	if N // 10 == 0:
		return N
	else:
		total = 0
		while N > 0:
			total += N % 10
			N //= 10
		return count_back(total)
lst = [int(i) for i in input().strip().split()]
(N, K) = lst
total = 0
while N > 0:
	total += N % 10
	N //= 10
total *= K
print(count_back(total))


def super_digit(num):
	if num // 10 == 0:
		return num
	else:
		return super_digit(sum(list(map(int, list(str(num))))))
(n, p) = map(int, input().strip().split(' '))
val = super_digit(sum(list(map(int, list(str(n))))) * p)
print(val)


(n, k) = [int(x) for x in input().split()]
while n >= 10:
	a = [int(x) for x in list(str(n))]
	n = sum(a)
n *= k
while n >= 10:
	a = [int(x) for x in list(str(n))]
	n = sum(a)
print(n)


def superDigit(n, k):
	if k == 1:
		if len(n) == 1:
			return int(n)
		m = 0
		for i in range(len(n)):
			m += int(n[i])
		return superDigit(str(m), k)
	else:
		k_1 = int(k / 2)
		k_2 = k - k_1
		m = 0
		for i in range(len(n)):
			m += int(n[i])
		return superDigit(str(superDigit(str(m), k_1)) + str(superDigit(str(m), k_2)), 1)
(n, k) = input().strip().split()
print(superDigit(n, int(k)))


def super_digit(P):
	if len(P) == 1:
		return P
	else:
		return super_digit(str(sum((int(digit_str) for digit_str in P))))
(n, k) = input().strip().split(' ')
if len(n) > 1:
	P = str(sum((int(digit_str) for digit_str in n)) * int(k))
elif len(n) == 1 and k == 1:
	print(n)
print(super_digit(P))


def digitsum(x_str):
	return sum(map(int, x_str))
(n, k) = input().split()
x = digitsum(n) * int(k)
while x >= 10:
	x = digitsum(str(x))
print(x)


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

def sum(p):
	if p < 10:
		return p
	else:
		s = 0
		while p != 0:
			s = s + p % 10
			p = p // 10
		return sum(s)
x = sum(x)
x = x * n
x = sum(x)
print(x)


def super_digit(n):
	if n < 10:
		return n
	sum = 0
	while n != 0:
		d = n % 10
		sum = sum + d
		n = n // 10
	return super_digit(sum)
(n, k) = input().split()
k = int(k)
n = int(n)
n = n * k
result = super_digit(n)
print(result)


import sys
import math
(pattern, reps) = sys.stdin.readline().split(' ')
reps = int(reps)
pattern = [int(digit) for digit in pattern]
sum_of_pattern = sum(pattern)

def sum_of_digits(num):
	n_digits = int(math.log10(num)) + 1
	if n_digits == 1:
		return num
	s = sum([int(num / 10 ** i % 10) for i in range(n_digits)])
	if s < 10:
		return s
	else:
		return sum_of_digits(s)
print(sum_of_digits(sum_of_pattern * reps))


(n, k) = list(map(int, input().strip().split(' ')))

def ds(x):
	if x < 10:
		return x
	a = str(x)
	if not a[-1].isdigit():
		a = x[:-1]
	b = sum((int(c) for c in a))
	if b > 9:
		return ds(b)
	return b
n_reduced = ds(n)
nrk = n_reduced * k
answ = ds(nrk)
print(answ)


(n, k) = (int(i) for i in input().split())
sd = n * k % 9
print(sd if sd else 9)


import sys

def superdigit(P):
	if len(P) == 1:
		return P
	else:
		return superdigit(str(sum([int(P[i]) for i in range(len(P))])))
(n, k) = input().strip().split(' ')
k = int(k)
P = n
n = superdigit(P)
n = superdigit(n * k)
print(n)


def ar(a):
	if a > 10:
		return ar(sum(map(int, list(str(a)))))
	else:
		return a
s = [int(i) for i in input().split()]
z = ar(ar(s[0]) * s[1])
print(z)


(n, k) = input().split(' ')
k = int(k)

def super_digit(n, k=1):
	s = sum((int(i) for i in n))
	s = s * k
	if s // 10:
		return super_digit(str(s))
	else:
		return s
print(super_digit(n, k))


def super_digit(N):
	if len(N) == 1:
		return int(N)
	else:
		return super_digit(str(sum((int(x) for x in N))))
(n, k) = [x for x in input().split()]
print(super_digit(str(super_digit(n) * int(k))))


def super_digit(num):
	if num // 10 == 0:
		return num
	else:
		return super_digit(sum(list(map(int, list(str(num))))))
(n, p) = map(int, input().strip().split(' '))
val = super_digit(sum(list(map(int, list(str(n))))) * p)
print(val)


(a, b) = list(map(str, input().strip().split()))
b = int(b)
c = 0
for i in a:
	c += int(i)
k = str(c)
k = k * b
while len(k) != 1:
	c = 0
	for i in k:
		c += int(i)
	k = str(c)
print(k)


def sum_of_digits(P):
	return sum(map(int, list(str(P))))

def super_digit(P, DP):
	if P < 10:
		return P
	else:
		if P in DP:
			return DP[P]
		sd = super_digit(sum_of_digits(P), DP)
		DP[P] = sd
		return sd
(n, k) = map(int, input().strip().split(' '))
P = sum_of_digits(n) * k
DP = {}
print(super_digit(P, DP))


(n, k) = map(int, input().split())
n = str(n)

def sup(a, k=1):
	if len(a) == 1:
		return int(a)
	s = 0
	for i in a:
		s += int(i)
	return sup(str(s * k))
print(sup(n, k))


(n, k) = map(int, input().split())
x = n * k % 9
print(x if x else 9)


def se(c):
	d = 0
	for q in str(c):
		d += int(q)
	return d
(s, k) = input().split()
k = int(k)
c = 0
for q in s:
	c = c + int(q)
c = c * k
d = 0
for q in str(c):
	d += int(q)
z = 0
while len(str(d)) != 1:
	d = se(d)
print(d)


(n, k) = [i for i in input().strip().split()]
digit = str(sum((int(i) for i in n)) * int(k))
digit = [i for i in digit]
while True:
	digit = str(sum((int(i) for i in digit)))
	digit = [i for i in digit]
	if len(digit) == 1:
		break
print(int(digit[0]))


(n, k) = input().split()
print((((sum(map(int, n)) - 1) % 9 + 1) * int(k) - 1) % 9 + 1)


class Solution(object):

	def super_digit(self, n, k):
		number = sum([int(y) for y in n]) * int(k)

		def __super_digit(x):
			if 0 < x < 10:
				return x
			nums = sum([int(y) for y in str(x)])
			return __super_digit(nums)
		return __super_digit(number)
(n, k) = input().split(' ')
s = Solution()
x = s.super_digit(n, k)
print(x)


(n, k) = map(int, input().split())
x = n * k % 9
print(x if x else 9)


def descomp(n):
	res = 0
	for i in range(len(n)):
		res += int(n[i])
	return res
inp = input().split()
k = int(inp[1])
n = int(inp[0])
while int(n) > 9:
	n = str(descomp(str(n)))
temp = n
for i in range(k - 1):
	n += temp
while int(n) > 9:
	n = descomp(str(n))
print(n)


(n, k) = map(int, input().split())
x = n * k % 9
print(x if x else 9)


(n, k) = input().split()
n = list(n)
n = str(sum(map(int, n)) * k)
while len(n) > 1:
	n = str(sum(map(int, n)))
print(n)


(n, k) = input().split(' ')
k = int(k)
digits = [int(x) for x in n]
first_sum = sum(digits) * k
digits = [int(x) for x in str(first_sum)]
while len(digits) > 1:
	s = sum(digits)
	digits = [int(x) for x in str(s)]
print(digits[0])


(n, k) = input().strip().split(' ')

def superdigit(ns, mu):
	if len(ns) < 2 and int(ns) * mu < 10:
		return str(int(ns) * mu)
	tot = sum((int(i) for i in ns))
	tot *= mu
	return superdigit(str(tot), 1)
print(superdigit(n, k))


(n, k) = input().strip().split()
(n, k) = (int(n), int(k))

def Super_digit(n, k):
	if len(str(n)) == 1 and k == 1:
		return n
	else:
		strN = str(n)
		count = 0
		for char in strN:
			count += int(char)
		if len(str(count)) == 1:
			return Super_digit(count * k, 1)
		else:
			return Super_digit(count, k)
print(Super_digit(n, k))


(n, k) = map(int, input().split())
x = n * k % 9
print(x if x else 9)


a = input().split(' ')
n = a[0]
k = int(a[1])

def splitter(a):
	if len(a) == 2:
		return str(int(a[0]) + int(a[1]))
	if len(a) == 1:
		return a
	num = len(a) // 2
	left = splitter(a[0:num])
	right = splitter(a[num:])
	return str(int(left) + int(right))

def super_digit2(a, concatnater):
	if len(a) == 1:
		a = str(a) * concatnater
		concatnater = 1
		if len(a) == 1:
			return int(a)
	a = splitter(a)
	return super_digit2(a, concatnater)
print(super_digit2(n, k))


def fun(s):
	if int(s) < 10:
		return int(s)
	else:
		sum = 0
		for i in s:
			sum += int(i)
		return fun(str(sum))
n = input().split()
sum = 0
for i in n[0]:
	sum += int(i)
sum *= int(n[1])
a = fun(str(sum))
print(a)