# 計算量 O(いっぱい) from collections import defaultdict, deque n = int(input()) s = input() cost = defaultdict(int) cost[s] = 0 goal = "".join(sorted(list(s))) que = deque([s]) while que: t = que.popleft() if t == goal: exit(print(cost[t])) t1 = t[-1] + t[:-1] t2 = t[0] + t[-1] + t[1:-1] if t1 not in cost: cost[t1] = cost[t] + 1 que.append(t1) if t2 not in cost: cost[t2] = cost[t] + 1 que.append(t2)