import numpy as np def solve_max(area): next_area = np.array(area) count = 0 while True: max_ind = np.argmax(next_area) min_ind = np.argmin(next_area) if max_ind == min_ind: break next_area = next_area[0:min_ind] if max_ind < min_ind else next_area[min_ind + 1: next_area.shape[0]] count += 1 return count def solve_min(area): next_area = np.array(area) count = 0 while True: max_ind = np.argmax(next_area) min_ind = np.argmin(next_area) if max_ind == min_ind: break next_area = next_area[0:max_ind] if max_ind > min_ind else next_area[max_ind + 1: next_area.shape[0]] count += 1 return count def solve(area): if area.shape[0] <= 1: return 0 count = solve_max(area) count += solve_min(area) - 1 max_ind = np.argmax(area) min_ind = np.argmin(area) left_ind = min_ind if min_ind < max_ind else max_ind right_ind = min_ind if min_ind > max_ind else max_ind count += solve(area[0:left_ind]) count += solve(area[left_ind+1: right_ind]) count += solve(area[right_ind+1:area.shape[0]]) return count N = int(input()) input_np = np.array(input().split()).astype(np.int32) print(solve(input_np))