N = int(input()) A = list(map(int, input().split())) ans = float('inf') first = A[0] second = min(A[1:]) for i, a in enumerate(A[1:-1]): if second == a: second = min(A[2+i:]) if (first - a) * (second - a) > 0: ans = min(ans, first + second + a) if first > a: first = a if ans == float('inf'): ans = -1 print(ans)