#include using namespace std; using lint = long long; const int INF = 1e9; signed main(){ int n; cin >> n; vector a(n), dp(n, INF); for(int i = 0; i < n; i++) scanf("%d", &a[i]); int ans = 0; for(int i = n - 1; i >= 0; i--){ if(a[i] == ans) ans--; } cout << ans << endl; }