n = int(input())
heights = map(int, input())

current_highest = heights[0]
steps_sum = 0
for h in heights[1:]:
  if h > current_highest:
    current_highest = h
  else:
    steps_sum += h - current_highest

print(steps_sum)