Stepan has a very big positive integer.

Let's consider all cyclic shifts of Stepan's integer (if we look at his integer like at a string) which are also integers (i.e. they do not have leading zeros). Let's call such shifts as good shifts. For example, for the integer 10203 the good shifts are the integer itself 10203 and integers 20310 and 31020.

Stepan wants to know the minimum remainder of the division by the given number m among all good shifts. Your task is to determine the minimum remainder of the division by m.


-----Input-----

The first line contains the integer which Stepan has. The length of Stepan's integer is between 2 and 200 000 digits, inclusive. It is guaranteed that Stepan's integer does not contain leading zeros.

The second line contains the integer m (2 ≤ m ≤ 10^8) — the number by which Stepan divides good shifts of his integer.


-----Output-----

Print the minimum remainder which Stepan can get if he divides all good shifts of his integer by the given number m.


-----Examples-----
Input
521
3

Output
2

Input
1001
5

Output
0

Input
5678901234567890123456789
10000

Output
123



-----Note-----

In the first example all good shifts of the integer 521 (good shifts are equal to 521, 215 and 152) has same remainder 2 when dividing by 3.

In the second example there are only two good shifts: the Stepan's integer itself and the shift by one position to the right. The integer itself is 1001 and the remainder after dividing it by 5 equals 1. The shift by one position to the right equals to 1100 and the remainder after dividing it by 5 equals 0, which is the minimum possible remainder.

Source: codeforces | Difficulty: HARD
Tags: Number theory

Mathematics


Solutions:
s = input()
m = int(input())
N = int(s)
MIN = N % m
L = len(s) - 1
temp1 = N
temp2 = 0
big = pow(10, L, m)
for i in range(0, len(s) - 1):
	temp2 *= 10
	temp2 += int(s[i])
	temp2 %= m
	temp1 -= big * int(s[i])
	temp1 *= 10
	temp1 %= m
	temp = temp1 + temp2
	temp %= m
	if s[i + 1] != '0':
		if temp < MIN:
			MIN = temp
print(MIN)


N = input()
M = int(input())
cur = int(N) % M
ans = cur
for i in range(len(N)):
	cur -= (ord(N[i]) - ord('0')) * pow(10, len(N) - 1, M) % M
	cur = cur * 10 % M
	cur = (cur + ord(N[i]) - ord('0')) % M
	if N[(i + 1) % len(N)] != '0':
		ans = min(ans, cur)
print(ans)


s = input()
m = int(input())
mn = m
ttt = 0
t = 0
ttt = 1
for i in range(1, len(s)):
	ttt = ttt * 10 % m
for i in range(0, len(s)):
	t = (t * 10 + ord(s[i]) - ord('0')) % m
for i in range(0, len(s)):
	if s[i] != '0':
		mn = min(mn, t)
	t = t - (ord(s[i]) - ord('0')) * ttt % m
	if t < 0:
		t = t + m
	t = (t * 10 + (ord(s[i]) - ord('0'))) % m
print(mn)


s = input()
m = int(input())
pw = [0] * (len(s) + 1)
pw[0] = 1
for i in range(1, len(s) + 1):
	pw[i] = pw[i - 1] * 10 % m
cur = 0
for i in range(len(s)):
	cur *= 10
	cur += ord(s[i]) - ord('0')
	cur %= m
ans = cur
for i in range(1, len(s)):
	cur *= 10
	cur %= m
	cur -= (ord(s[i - 1]) - ord('0')) * pw[len(s)] % m
	cur += m
	cur %= m
	cur += ord(s[i - 1]) - ord('0')
	cur %= m
	if s[i] != '0':
		ans = min(ans, cur)
print(ans)


n = input()
m = int(input())
res = 0
pw = 1
for i in range(len(n) - 1, -1, -1):
	res = (res + pw * int(n[i])) % m
	if i > 0:
		pw = pw * 10 % m
partial = res
for x in n:
	if int(x) != 0:
		res = min(res, partial)
	partial = (partial - int(x) * pw) % m
	partial = (partial * 10 + int(x)) % m
print(res)