You got a box with a combination lock. The lock has a display showing n digits. There are two buttons on the box, each button changes digits on the display. You have quickly discovered that the first button adds 1 to all the digits (all digits 9 become digits 0), and the second button shifts all the digits on the display one position to the right (the last digit becomes the first one). For example, if the display is currently showing number 579, then if we push the first button, the display will show 680, and if after that we push the second button, the display will show 068.

You know that the lock will open if the display is showing the smallest possible number that can be obtained by pushing the buttons in some order. The leading zeros are ignored while comparing numbers. Now your task is to find the desired number.


-----Input-----

The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of digits on the display.

The second line contains n digits — the initial state of the display.


-----Output-----

Print a single line containing n digits — the desired state of the display containing the smallest possible number.


-----Examples-----
Input
3
579

Output
024

Input
4
2014

Output
0142

Difficulty: interview

Starter code:

Solutions:

def modify(string, index):
    
    if string[index] == '0':
        key = 0
    else:
        key = 10 - int(string[index])
    bad = ''
    for i in string:
        bad += str((int(i) + key) % 10)
    return bad[index:] + bad[:index]


x = int(input())
y = input()
minx = 'zzzzzzzzz'
for i in range(x):
    minx = min(minx, modify(y, i))
print(minx)


n = int(input())
l = list(map(int, list(input())))

def minit(l):
    dec = l[0]
    for i in range(len(l)):
        l[i] = (l[i] - dec + 10) % 10
    return l

ans = [float('+inf')]

for i in range(n):
    mm = minit(l[:])
    ans = min(mm, ans)
    l = l[1:] + [l[0]]

print(''.join(map(str, ans)))


n = int(input())
num = input()

ans = str(num)
for i in range(n):
    d = int(num[i])
    new_num = ''
    for j in range(n):
        dig = (int(num[(i + j) % n]) - d) % 10
        new_num += str(dig)
    ans = min(ans, new_num)

print(ans)


n = int(input())
d = input()
c = d
s = '1'
m = 10**1005
for k in range(0,11):
    for i in range(0,n):
        c = c[:i]+str((int(c[i])+1)%10)+c[i+1:]
    for i in range(0,n):
        c = c[1:]+c[0]
        b = int(c)
        if b < m:
            m = b
            s = c
print(s)


def main(n,a):
    a = ("{:0"+ ("{}".format(n)) + "d}").format(int(a))
    mn = int(a)
    for j in range(n):
        x = list(a[j:]+a[:j])
        y = x.copy()
        #for i in range(10):
        i = 10-int(y[0])
        x = y.copy()
        for k in range(n):
            x[k] = str((int(x[k])+i)%10)
        #print(j,i,x, mn)
        if int("".join(x)) < mn:
            mn = int("".join(x))

    print(("{:0"+ ("{}".format(n)) + "d}").format(mn))


def main_input():
    n = int(input())
    a = int(input())
    main(n,a)

def __starting_point():
    main_input()

__starting_point()

# B

n = int(input())
l = list(input())
for i in range(n):
    l[i] = int(l[i])
ans = 10**(n-1)
for i in range(n):
    l = l[1:]+l[:1]
    m = []
    for j in range(n):
        m.append(l[j])
    for j in range(1,n):
        m[j] -= m[0]
        if m[j] < 0:
            m[j] += 10
    m[0] = 0
    cur = ''
    for j in range(n):
        cur += str(m[j])
    ans = min(ans, int(cur))
ans = str(ans)
while len(ans) < n:
    ans = '0'+ans
print(ans)


n = int(input())
num = input()
status = 0
check = num[0]
for i in num:
    if i != check:
        status = 1
        break
if status == 0:
    for i in range(n):
        print(0,end="")
    print()
else:
    maximum = 0
    consider = []
    for i in range(10):
        dum = ""
        for j in range(n):
            dum = dum + (str(((int(num[j]) + i) % 10)))
        count = 0
        cur = 0
        ind = []
        for j in range(n):
            if dum[j] != '0':
                if cur > count:
                    count = cur
                    ind = [j-count]
                elif cur == count:
                    ind.append(j-count)
                cur = 0
            else:
                cur += 1
        if cur != 0:
            if cur > count:
                count = cur
                ind = [j-count+1]
            elif cur == count:
                ind.append(j-count+1)
        if count > maximum:
            consider = []
            for i in ind:
                consider.append([dum,i])
                maximum = count
        elif count == maximum:
            for i in ind:
                consider.append([dum,i])
    out = []

    for i in range(len(consider)):
        out.append(consider[i][0][consider[i][1]:] + consider[i][0][:consider[i][1]])
    print(min(out))


n = int(input())
d = input()
s = []
for i in d:
    s.append(int(i))
pos = 0
ans = 10 ** 1000
for i in range(1, len(s)):
    if s[i] == s[i - 1]:
        continue
    else:
        x = s[pos]
        t = [s[j] for j in range(len(s))]
        for j in range(len(t)):
            t[j] = (t[j] + 10 - x) % 10
        A = [0] * n
        for j in range(len(t)):
            A[j] = t[(j + pos) % n]
        a = int(''.join(list(map(str, A))))
        if a < ans:
            ans = a
        pos = i
x = s[pos]
for i in range(len(s)):
    s[i] = (s[i] + 10 - x) % 10
A = [0] * n
for i in range(len(s)):
    A[i] = s[(i + pos) % n]
a = int(''.join(list(map(str, A))))
if a < ans:
    ans = a
print('0' * (n - len(str(ans))) + str(ans))


__author__ = 'Rakshak.R.Hegde'
"""
Created on Dec 17 2014 PM 10:13

@author: Rakshak.R.Hegde
"""
n = int(input())
digits = list(map(int, input()))
enum = [[0] * n for i in range(10)]
enum[0] = digits
for i in range(1, 10):
    for j in range(n):
        enum[i][j] = (enum[i - 1][j] + 1) % 10
comp = []
for i in range(10):
    if enum[i][0] == 0: comp.append(enum[i])
    for j in range(1, n):
        if enum[i][j] == 0 and enum[i][j - 1] != 0:
            comp.append(enum[i][j:] + enum[i][:j])
print(''.join(map(str, min(comp))))

n = int(input())
num = input()
num = [list(num) for _ in range(10)]
for i in range(10):
    for j in range(n):
        num[i][j] = (int(num[i][j]) + i) % 10

col = []
for i in range(10):
    for j in range(n):
        col.append(num[i][j:]+num[i][:j])

ans = ''.join(list(map(str, min(col))))
print(ans)



from itertools import chain


def main():
    n = int(input())
    aa = list(map(int, input()))
    res = []
    for j in range(len(aa)):
        y, base = 0, aa[j]
        for x in chain(aa[j:], aa[:j]):
            y = y * 10 + (x - base) % 10
        res.append(y)
    f = str(len(aa)).join(('{:0>', 'd}'))
    print(f.format(min(res)))


def __starting_point():
    main()
__starting_point()

def norm(s):
    return [(x + 10 - s[0]) % 10 for x in s]

n, = list(map(int, input().split()))
s = input().strip()
s = [int(x) for x in s]
print("".join(map(str, min([norm(s[j:] + s[:j]) for j in range(n)]))))


n = int(input())
s = input()
s = [int(x) for x in s]
print("".join(map(str, min([[(x + 10 - s[j]) % 10 for x in (s[j:] + s[:j])] for j in range(n)]))))

n = int(input())
s = [int(x) for x in input()]
print("".join(map(str, min([[(x + 10 - s[j]) % 10 for x in (s[j:] + s[:j])] for j in range(n)]))))

n = int(input())
c = list(map(int, input()))
val = list(c)
for i in range(n):
    d = (10 - c[0]) % 10
    for j in range(n):
        c[j] = (c[j] + d) % 10
    if c < val:
        val = list(c)
    c.append(c[0])
    del(c[0])
print(''.join(map(str, val)))

from itertools import chain
C=0

def main():
    input()
    s = input()
    n = len(s)
    a = s[-1]
    for stop, b in enumerate(s):
        if b != a:
            break
    else:
        print('0' * n)
        return
    s = ''.join((s[stop:], s[:stop]))
    a, start = s[0], 0
    ma, l = 0, []
    for stop, b in enumerate(s):
        if b != a:
            le = stop - start
            #tail=9-(ord(b)-ord(a))%10
            if ma < le:
                ma = le
                l.clear()
                l.append(start)
            elif ma == le:
                l.append(start)
            a, start = b, stop
    le = n - start
    if ma < le:
        l.clear()
        l.append(start)
    elif ma == le:
        l.append(start)
    for i, start in enumerate(l):
        base = int(s[start])
        l[i] = ''.join(chr((int(c) - base) % 10 + 48) for c in chain(s[start:], s[:start]))
    print(min(l))


def __starting_point():
    main()
__starting_point()

from itertools import chain

C = 0


def main():
    input()
    s = input()
    n = len(s)
    a = s[-1]
    for stop, b in enumerate(s):
        if b != a:
            break
    else:
        print('0' * n)
        return
    s = ''.join((s[stop:], s[:stop]))
    a, l = s[0], []
    start = ma = tail = 0
    for stop, b in enumerate(s):
        if b != a:
            le = stop - start
            if ma < le:
                ma, tail = le, (ord(b) - ord(a)) % 10
                l.clear()
                l.append(start)
            elif ma == le:
                tl = (ord(b) - ord(a)) % 10
                if tail > tl:
                    tail = tl
                    l.clear()
                    l.append(start)
                elif tail == tl:
                    l.append(start)
            a, start = b, stop
    le = n - start
    if ma < le:
        l.clear()
        l.append(start)
    elif ma == le:
        l.append(start)
    for i, start in enumerate(l):
        base = int(s[start])
        l[i] = ''.join(chr((int(c) - base) % 10 + 48) for c in chain(s[start:], s[:start]))
    print(min(l))


def __starting_point():
    main()
__starting_point()

from itertools import chain

C = 0


def main():
    input()
    ls = list(map(int, input()))
    n = len(ls)
    a = ls[-1]
    for stop, b in enumerate(ls):
        if b != a:
            break
    else:
        print('0' * n)
        return
    ls = ls[stop:] + ls[:stop]
    a, l = ls[0], []
    start = ma = tail = 0
    for stop, b in enumerate(ls):
        if b != a:
            le = stop - start
            if ma < le:
                ma, tail = le, (b - a) % 10
                l.clear()
                l.append(start)
            elif ma == le:
                tl = (b - a) % 10
                if tail > tl:
                    tail = tl
                    l.clear()
                    l.append(start)
                elif tail == tl:
                    l.append(start)
            a, start = b, stop
    le = n - start
    if ma < le:
        l.clear()
        l.append(start)
    elif ma == le:
        tl = (ls[0] - a) % 10
        if tail > tl:
            l.clear()
            l.append(start)
        elif tail == tl:
            l.append(start)
    for i, start in enumerate(l):
        base = ls[start]
        l[i] = ''.join(chr((a - base) % 10 + 48) for a in chain(ls[start:], ls[:start]))
    print(min(l))


def __starting_point():
    main()
__starting_point()

from itertools import chain

C = 0


def main():
    input()
    ls = list(map(int, input()))
    n = len(ls)
    a = ls[-1]
    for stop, b in enumerate(ls):
        if b != a:
            break
    else:
        print('0' * n)
        return
    ls = ls[stop:] + ls[:stop]
    a, l = ls[0], []
    start = ma = tail = 0
    for stop, b in enumerate(ls):
        if b != a:
            le = stop - start
            if ma < le:
                ma, tail = le, (b - a) % 10
                l.clear()
                l.append(start)
            elif ma == le:
                tl = (b - a) % 10
                if tail > tl:
                    tail = tl
                    l.clear()
                    l.append(start)
                elif tail == tl:
                    l.append(start)
            a, start = b, stop
    le = n - start
    if ma < le:
        l.clear()
        l.append(start)
    elif ma == le:
        tl = (ls[0] - a) % 10
        if tail > tl:
            l.clear()
            l.append(start)
        elif tail == tl:
            l.append(start)
    for i, start in enumerate(l):
        base = ls[start]
        l[i] = ''.join(str((a - base) % 10) for a in chain(ls[start:], ls[:start]))
    print(min(l))


def __starting_point():
    main()
__starting_point()

#!/usr/bin/env python3

n = int(input())
state = [int(c) for c in input()]
min_s = state
for i in range(n):
    tmp = state[i:] + state[:i]
    tmp_min = [tmp[i] - tmp[0] for i in range(n)]
    for i in range(n):
        if tmp_min[i] < 0:
            tmp_min[i] += 10
    if tmp_min < min_s:
        min_s = tmp_min
print("".join(map(str, min_s)))


from itertools import chain

C = 0


def main():
    input()
    ls = list(map(int, input()))
    n = len(ls)
    a = ls[-1]
    for stop, b in enumerate(ls):
        if b != a:
            break
    else:
        print('0' * n)
        return
    ls = ls[stop:] + ls[:stop]
    a, l = ls[0], []
    start = ma = tail = 0
    for stop, b in enumerate(ls):
        if b != a:
            le = stop - start
            if ma < le:
                ma, tail = le, (b - a) % 10
                l.clear()
                l.append(start)
            elif ma == le:
                tl = (b - a) % 10
                if tail > tl:
                    tail = tl
                    l.clear()
                    l.append(start)
                elif tail == tl:
                    l.append(start)
            a, start = b, stop
    le = n - start
    if ma < le:
        l.clear()
        l.append(start)
    elif ma == le:
        tl = (ls[0] - a) % 10
        if tail > tl:
            l.clear()
            l.append(start)
        elif tail == tl:
            l.append(start)
    for i, start in enumerate(l):
        base = ls[start]
        l[i] = tuple((a - base) % 10 + 48 for a in chain(ls[start:], ls[:start]))
    print(''.join(map(chr, min(l))))


def __starting_point():
    main()
__starting_point()

def main():
    n = int(input())
    digits = list(map(int, input()))
    enum = [[0] * n for i in range(10)]
    enum[0] = digits
    for i in range(1, 10):
        for j in range(n):
            enum[i][j] = (enum[i - 1][j] + 1) % 10
    comp = []
    for i in range(10):
        if enum[i][0] == 0:
            comp.append(enum[i])
        for j in range(1, n):
            if enum[i][j] == 0 and enum[i][j - 1] != 0:
                comp.append(enum[i][j:] + enum[i][:j])
    print(''.join(map(str, min(comp))))


def __starting_point():
    main()
__starting_point()

def main():
    n = int(input())
    digits = list(map(int, input()))
    enum = [digits]
    for a in range(1, 10):
        enum.append([(a + b) % 10 for b in digits])
    comp = []
    for i in range(10):
        if enum[i][0] == 0:
            comp.append(enum[i])
        for j in range(1, n):
            if enum[i][j] == 0 and enum[i][j - 1] != 0:
                comp.append(enum[i][j:] + enum[i][:j])
    print(''.join(map(str, min(comp))))


def __starting_point():
    main()
__starting_point()

def main():
    n = int(input())
    digits = list(map(int, input()))
    enum = [digits]
    for a in range(1, 10):
        enum.append([(a + b) % 10 for b in digits])
    for digits in enum[:10]:
        for j in range(n):
            if digits[j - 1] and not digits[j]:
                enum.append(digits[j:] + digits[:j])
    print(''.join(map(str, min(enum))))


def __starting_point():
    main()
__starting_point()

def main():
    input()
    digits = list(map(int, input()))
    l = [digits]
    for a in range(1, 10):
        l.append([(a + b) % 10 for b in digits])
    for digits in l[:]:
        a = digits[-1]
        for i, b in enumerate(digits):
            if a and not b:
                l.append(digits[i:] + digits[:i])
            a = b
    print(''.join(map(str, min(l))))


def __starting_point():
    main()
__starting_point()