#include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; long long MOD = 1000000007; int main() { int N; cin >> N; vector A(N); for ( int i = 0; i < N; i++ ) { cin >> A[i]; } int x = 0; for ( int i = N-1; i >= 0; i-- ) { if ( A[i] != i+1 ) { x = i; break; } } if ( A[0] == x ) { x--; } cout << x << endl; return 0; }