#include using namespace std; signed main(){ int N; cin >> N; vector< int > pos( N + 1 ); for( int i = 0; i < N; ++i ){ int A; cin >> A; pos[ A ] = i; } for( int i = N - 1; i >= 1; --i ) if( pos[ i ] > pos[ i + 1 ] ) cout << i << endl, exit( 0 ); cout << 0 << endl; return 0; }