def fnc(d,n=0): if "".join(map(str,d)) == "".join(map(str,sorted(d))): print(n) else: x = d[0]-1 if d[0] == min(d): x = max(d)-1 d.remove(x) d.insert(0,x) fnc(d, n+1) input() fnc(list(map(int,input().split(" "))))