#include "bits/stdc++.h" using namespace std; typedef long long ll; int main() { int n;cin >> n; vector a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } int ans = 1000000000; for (int i = 0; i < n-2; i++) { if (a[i] < a[i + 1] && a[i + 1] > a[i + 2]) { ans = min(ans,a[i] + a[i + 1] + a[i +2]); } else if (a[i] > a[i + 1] && a[i + 1] < a[i + 2]) { ans = min(ans,a[i] + a[i +1] + a[i +2]); } } if (ans == 1000000000) { cout<< -1 << endl; } else { cout << ans << endl; } return 0; }