def hyx(s: str, index: int):#实现数字互换 x = list(s) x[index], x[index + 1] = x[index + 1], x[index] s = "".join(x) return s def maya(n, x: str):#求解最少次数 if "2012" in x: return 0 if x.count("2") < 2 or x.count("1") < 1 or x.count("0") < 1: return -1 ct = 0 ans = [] ans.append(x) zc = [] while True: ans += zc for s in ans:#遍历每一层 for i in range(len(s) - 1):#每一层内遍历检查 if "2012" in s: return ct zc.append(hyx(s, i)) ct += 1 while True: try: n = int(input()) s = input() print(maya(n, s)) except: break