stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include <bits/stdc++.h>                                           

 

//#define int long long

//#pragma GCC optimize("Ofast")

//#pragma comment(linker, "/stack:200000000")

//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")

 

 

#define file(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout);

#define forev(i, b, a) for(int i = (b); i >= (a); --i)

#define forn(i, a, b) for(int i = (a); i <= (b); ++i)

#define all(x) x.begin(), x.end()

#define sz(s) (int)s.size()

#define pb push_back

#define ppb pop_back

#define s second

#define f first

 

 

using namespace std;

 

 

typedef pair < long long, long long > pll;    

typedef pair < int, int > pii;

typedef unsigned long long ull;         

typedef vector < pii > vpii;

typedef vector < int > vi;

typedef long double ldb;  

typedef long long ll;  

typedef double db;                      

 

 

const int inf = 1e9, maxn = 4e5 + 148, mod = 1e9 + 7, N = 1e5 + 11;

const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}, block = 555;

const pii base = {1171, 3307}, Mod = {1e9 + 7, 1e9 + 9};

const db eps = 1e-12, pi = 3.14159265359;

const ll INF = 1e18;





int n, v, u, dp[N][2];

vi g[N];





void dfs (int v, int p = 0) {

	dp[v][0] = 1;

	dp[v][1] = 1;



	for (auto to : g[v])

		if (to != p) {

			dfs(to, v);

			dp[v][0] = dp[v][0] * 1ll * dp[to][1] % mod;

			dp[v][1] = dp[v][1] * 1ll * (dp[to][0] + dp[to][1]) % mod;

		}

}





main () {

	cin >> n;

	forn (i, 1, n - 1) {

		cin >> v >> u;

		g[v].pb(u);

		g[u].pb(v);

	}



	dfs(1);

	cout << (dp[1][0] + dp[1][1]) % mod;

}



    

// Optimized solution to the same problem (faster):
#include<iostream>

#include<cstdio>

using namespace std;

typedef long long ll;

const int Maxn=1e5+5;

const int Mod=1e9+7;

struct edge{

	int v,nx;

}e[Maxn<<1];

int n,ne,f[Maxn];

ll dp[Maxn][2];

void read(int u,int v)

{	e[++ne].v=v;

	e[ne].nx=f[u];

	f[u]=ne;

}

void dfs(int u,int fa)

{	dp[u][0]=dp[u][1]=1ll;

	for(int i=f[u];i;i=e[i].nx)

	{	int v=e[i].v;

		if(v==fa)continue;

		dfs(v,u);

		dp[u][0]=((dp[v][1]+dp[v][0])%Mod*dp[u][0])%Mod;

		dp[u][1]=(dp[u][1]*dp[v][0])%Mod;

	}

}

int main()

{	scanf("%d",&n);

	for(int i=1;i<n;i++)

	{	int u,v;

		scanf("%d%d",&u,&v);

		read(u,v);read(v,u);

	}

	dfs(1,0);

	printf("%lld\n",(dp[1][0]+dp[1][1])%Mod);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
//#pragma GCC target ("avx2,sse2")

//#pragma GCC optimization ("Ofast")

//#pragma GCC optimization ("unroll-loops")

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;



#define ordered_set tree <pair <ll, int>, null_type, less <pair <ll, int> >, rb_tree_tag, tree_order_statistics_node_update>

#define ll long long

#define ull unsigned long long

#define db double

#define pb push_back

#define pf push_front

#define ppb pop_back

#define ppf pop_front

#define fi first

#define se second

#define mp make_pair

#define up_b upper_bound

#define low_b lower_bound

#define sz(x) (int)(x).size()

#define all(x) (x).begin(), (x).end()

#define endl "\n"



#define left fsdsdfoisf

#define sum dpsdfioppsf

#define assign xcvjlkdjfio

#define trie fksdfkjkfnjuiv

#define next sidlfjsfkl

#define merge sdfksdkfsldf



#define int long long



using namespace std;



void dout() {

    cerr << endl;

}

template <typename Head, typename... Tail>

void dout(Head H, Tail... T) {

    cerr << H << ' ';

    dout(T...);

}



//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef pair <int, int> pii;



const int N = 1e5 + 7, MOD = 1e9 + 7;

int n, dp[2][N];

vector <int> q[N];



void dfs(int v, int p) {

    int child = 0, A = 1, B = 1;

    for (auto i : q[v]) {

        if (i == p) {

            continue;

        }

        dfs(i, v);

        A = A * (dp[0][i] + dp[1][i]) % MOD;

        B = B * dp[0][i] % MOD;

        child++;

    }

    if (child == 0) {

        dp[0][v] = dp[1][v] = 1;

        return;

    }

    dp[0][v] = A;

    dp[1][v] = B;

}



void solve(int tc) {

    // check for (int i = 0; i < n; j++)

    cin >> n;

    for (int i = 1; i <= n - 1; i++) {

        int x, y;

        cin >> x >> y;

        q[x].pb(y);

        q[y].pb(x);

    }

    dfs(1, 1);

    cout << (dp[0][1] + dp[1][1]) % MOD;

}



signed main() {

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

//    freopen("input.txt", "r", stdin);

//    freopen("output.txt", "w", stdout);

    int tc = 1;

//    cin >> tc;

    for (int i = 0; i < tc; i++) {

        solve(i);

//        cleanup();

    }

}


// Optimized solution to the same problem (faster):
#include<iostream>

#include<cstdio>

using namespace std;

typedef long long ll;

const int Maxn=1e5+5;

const int Mod=1e9+7;

struct edge{

	int v,nx;

}e[Maxn<<1];

int n,ne,f[Maxn];

ll dp[Maxn][2];

void read(int u,int v)

{	e[++ne].v=v;

	e[ne].nx=f[u];

	f[u]=ne;

}

void dfs(int u,int fa)

{	dp[u][0]=dp[u][1]=1ll;

	for(int i=f[u];i;i=e[i].nx)

	{	int v=e[i].v;

		if(v==fa)continue;

		dfs(v,u);

		dp[u][0]=((dp[v][1]+dp[v][0])%Mod*dp[u][0])%Mod;

		dp[u][1]=(dp[u][1]*dp[v][0])%Mod;

	}

}

int main()

{	scanf("%d",&n);

	for(int i=1;i<n;i++)

	{	int u,v;

		scanf("%d%d",&u,&v);

		read(u,v);read(v,u);

	}

	dfs(1,0);

	printf("%lld\n",(dp[1][0]+dp[1][1])%Mod);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

const int N=100005;

const int mod=1000000007;

long long dp[N][2];

vector<int>e[N];

void dfs(int u,int f)

{

	dp[u][0]=dp[u][1]=1;

	for(int i=0;i<e[u].size();i++)

	{

		int v=e[u][i];

		if(v!=f)

		{

			dfs(v,u);

			dp[u][0]=dp[u][0]*dp[v][1]%mod;

			dp[u][1]=dp[u][1]*(dp[v][0]+dp[v][1])%mod;

		}

	}

}

int main()

{

	int n;

	scanf("%d",&n);

	for(int i=1;i<n;i++)

	{

		int x,y;

		scanf("%d%d",&x,&y);

		e[x].push_back(y);

		e[y].push_back(x);

	}

	dfs(1,0);

	printf("%d\n",(dp[1][0]+dp[1][1])%mod);

	return 0;

}


// Optimized solution to the same problem (faster):
#include<iostream>

#include<cstdio>

using namespace std;

typedef long long ll;

const int Maxn=1e5+5;

const int Mod=1e9+7;

struct edge{

	int v,nx;

}e[Maxn<<1];

int n,ne,f[Maxn];

ll dp[Maxn][2];

void read(int u,int v)

{	e[++ne].v=v;

	e[ne].nx=f[u];

	f[u]=ne;

}

void dfs(int u,int fa)

{	dp[u][0]=dp[u][1]=1ll;

	for(int i=f[u];i;i=e[i].nx)

	{	int v=e[i].v;

		if(v==fa)continue;

		dfs(v,u);

		dp[u][0]=((dp[v][1]+dp[v][0])%Mod*dp[u][0])%Mod;

		dp[u][1]=(dp[u][1]*dp[v][0])%Mod;

	}

}

int main()

{	scanf("%d",&n);

	for(int i=1;i<n;i++)

	{	int u,v;

		scanf("%d%d",&u,&v);

		read(u,v);read(v,u);

	}

	dfs(1,0);

	printf("%lld\n",(dp[1][0]+dp[1][1])%Mod);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
//       Author: Mostafa Mounir Shehab

 

#include <bits/stdc++.h>

using namespace std;

 

#define PI  acos(-1)

#define EPS (1e-10)

#define endl "\n"

#define SZ(v) (int)(v.size())

#define IO ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL), cerr.tie(NULL);

typedef long long ll;

typedef long double ld;

 



 

ll MOD=1e9 + 7;

ll OO=1e15;

const int N=1e5 + 5,M=1e5 + 5;

int arr[N];

ll dp[N][3];





int n,m,k,cnt,SU,idx,len,MN,MX,T;

int l,r,mid;

int a,b,c;

int ans;



vector<int> adj[N];



ll Solve(int u,int p,bool color){

	

	ll &ret = dp[u][color];

	if(~ret)

		return ret;

	ret = 1;

	

	for(int i:adj[u]){

		if(i == p) continue;

		

		ll val = Solve(i,u,true);

		if(color)

			val += Solve(i,u,false);

		

		ret*=val;

		ret%=MOD;

	}



	return ret;

}









int main(){

	IO

	

	memset(dp,-1,sizeof dp);

	

	cin>>n;

	for(int i=1;i<n;++i){

		cin>>a>>b;

		adj[a].push_back(b);

		adj[b].push_back(a);

	}

	

	ll val = Solve(1,-1,0)+Solve(1,-1,1);

	val%= MOD;

	

	

	cout<<val<<endl;

	

	

}


// Optimized solution to the same problem (faster):
#include<iostream>

#include<cstdio>

using namespace std;

typedef long long ll;

const int Maxn=1e5+5;

const int Mod=1e9+7;

struct edge{

	int v,nx;

}e[Maxn<<1];

int n,ne,f[Maxn];

ll dp[Maxn][2];

void read(int u,int v)

{	e[++ne].v=v;

	e[ne].nx=f[u];

	f[u]=ne;

}

void dfs(int u,int fa)

{	dp[u][0]=dp[u][1]=1ll;

	for(int i=f[u];i;i=e[i].nx)

	{	int v=e[i].v;

		if(v==fa)continue;

		dfs(v,u);

		dp[u][0]=((dp[v][1]+dp[v][0])%Mod*dp[u][0])%Mod;

		dp[u][1]=(dp[u][1]*dp[v][0])%Mod;

	}

}

int main()

{	scanf("%d",&n);

	for(int i=1;i<n;i++)

	{	int u,v;

		scanf("%d%d",&u,&v);

		read(u,v);read(v,u);

	}

	dfs(1,0);

	printf("%lld\n",(dp[1][0]+dp[1][1])%Mod);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
/*

    if you can't see the repay

    Why not just work step by step

    rubbish is relaxed

    to ljq

*/

#include <cstdio>

#include <cstring>

#include <iostream>

#include <queue>

#include <cmath>

#include <map>

#include <stack>

#include <set>

#include <sstream>

#include <vector>

#include <stdlib.h>

#include <algorithm>

using namespace std;

typedef long long ll;

const ll mod =  (int)1e9+7;

const int MAX_N = 100025;

int eid,p[MAX_N];

ll dp[MAX_N][2];

struct edge

{

    int v,next;

}e[MAX_N<<2];

void init()

{

    memset(p,-1,sizeof(p));

    eid = 0;

}

void add(int u,int v)

{

    e[eid].v = v;

    e[eid].next =p[u];

    p[u] = eid++;

}

void dfs(int u,int fa)

{

    ll x = 1, y = 1;

    if(e[p[u]].next==-1&&e[p[u]].v == fa)

    {

        dp[u][0] = dp[u][1] = 1;

        return ;

    }

    for(int i = p[u];i+1;i=e[i].next)

    {

        int v = e[i].v;

        if(v==fa) continue;

        dfs(v,u);

        x = (x*((dp[v][0] + dp[v][1])%mod))%mod;

        y = (y*(dp[v][0]))%mod;

    }

    dp[u][0] = x,dp[u][1] = y;

}

int main()

{

    //ios::sync_with_stdio(false);

    //freopen("a.txt","r",stdin);

    //freopen("b.txt","w",stdout);

    int n,a,b;

    init();

    scanf("%d",&n);

    for(int i = 1;i<n;++i)

    {

        scanf("%d%d",&a,&b);

        add(a,b);

        add(b,a);

    }

    dfs(1,0);

    printf("%d\n",(dp[1][0]+dp[1][1])%mod);

    //fclose(stdin);

    //fclose(stdout);

    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;

    return 0;

}




// Optimized solution to the same problem (faster):
#include<iostream>

#include<cstdio>

using namespace std;

typedef long long ll;

const int Maxn=1e5+5;

const int Mod=1e9+7;

struct edge{

	int v,nx;

}e[Maxn<<1];

int n,ne,f[Maxn];

ll dp[Maxn][2];

void read(int u,int v)

{	e[++ne].v=v;

	e[ne].nx=f[u];

	f[u]=ne;

}

void dfs(int u,int fa)

{	dp[u][0]=dp[u][1]=1ll;

	for(int i=f[u];i;i=e[i].nx)

	{	int v=e[i].v;

		if(v==fa)continue;

		dfs(v,u);

		dp[u][0]=((dp[v][1]+dp[v][0])%Mod*dp[u][0])%Mod;

		dp[u][1]=(dp[u][1]*dp[v][0])%Mod;

	}

}

int main()

{	scanf("%d",&n);

	for(int i=1;i<n;i++)

	{	int u,v;

		scanf("%d%d",&u,&v);

		read(u,v);read(v,u);

	}

	dfs(1,0);

	printf("%lld\n",(dp[1][0]+dp[1][1])%Mod);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



typedef unsigned long long ull;

typedef long long ll;

typedef pair<int, int> pii;

typedef pair<ll, ll> pll;

typedef pair<double, double> pdd;

const ull mod = 1e9 + 7;

#define REP(i,n) for(int i=0;i<(int)n;++i)



//debug

#define dump(x)  cerr << #x << " = " << (x) << endl;

#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;



template < typename T >

void vprint(T &v){

	REP(i, v.size()){

		cout << v[i] << " ";

	}

	cout << endl;

}



ll N;

vector<ll> G[101010];



ll dp[101010][2];



ll rec(ll now, ll par, ll white){

	if(dp[now][white]>=0) return dp[now][white];

	ll res = 1;

	REP(i, G[now].size()){

		ll next = G[now][i];

		if(next==par) continue;

		if(white==1){

			ll prod = rec(next, now, 1) + rec(next, now, 0);

			prod %= mod;

			res *= prod;

			res %= mod;

		}else{

			ll prod = rec(next, now, 1);

			prod %= mod;

			res *= prod;

			res %= mod;

		}

	}

	dp[now][white] = res;

	return res;

}



int main(){

	cin >> N;

	REP(i, N-1){

		ll x, y;

		cin >> x >> y;

		x--;y--;

		G[x].push_back(y);

		G[y].push_back(x);

	}

	REP(i, 101010)REP(j, 2) dp[i][j] = -1;

	cout << (rec(0, -1, 1) + rec(0, -1, 0))%mod << endl;

    return 0;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;



typedef unsigned long long ull;

typedef long long ll;

typedef pair<int, int> pii;

typedef pair<ll, ll> pll;

typedef pair<double, double> pdd;

const ull mod = 1e9 + 7;

#define REP(i,n) for(int i=0;i<(int)n;++i)



//debug

#define dump(x)  cerr << #x << " = " << (x) << endl;

#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;



template<class S, class T> ostream& operator << (ostream& os, const pair<S, T> v){

  os << "(" << v.first << ", " << v.second << ")"; return os;

}

template<class T> ostream& operator << (ostream& os, const vector<T> v){

  for(int i = 0; i < (int)v.size(); i++){if(i > 0){os << " ";} os << v[i];} return os;

}

template<class T> ostream& operator << (ostream& os, const vector<vector<T>> v){

  for(int i = 0; i < (int)v.size(); i++){if(i > 0){os << endl;} os << v[i];} return os;

}



ll N;

vector<ll> G[101010];



pll dfs(ll now, ll par){

	ll w=1, b=1;

	REP(i, G[now].size()){

		ll next = G[now][i];

		if(next == par) continue;

		pll res = dfs(next, now);



		ll cw = res.first;

		ll cb = res.second;



		w *= (cw+cb);

		w %= mod;



		b *= cw;

		b %= mod;

	}

	pll val = {w, b};

	return val;

}



int main(){

    cin.tie(0);

    ios::sync_with_stdio(false);



    cin >> N;

    REP(i, N-1){

    	ll x, y;

    	cin >> x >> y;

    	x--, y--;

    	G[x].push_back(y);

    	G[y].push_back(x);

    }



    auto res = dfs(0, -1);

    cout << (res.first + res.second)%mod << endl;

    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

const int N = 2e5 + 3;

int n, a[N];

vector<int> occ[N]; // occurrence vectors

int main() {

  	cin >> n;

  	for (int i = 0; i < n; ++i) {

      	cin >> a[i];

    	occ[a[i]].push_back(i);

    }

  	int prev = -1, taken = 0;

  	for (int val = 1; val <= n; ++val) {

  		bool found = false;

    	for (auto idx: occ[val]) {

        	if (idx > prev) {

             	prev = idx; 

              	taken++;

              	found = true;

             	break;

            }

        }

        if (!found)

        	break;

    }

  	if (taken == 0)

      	cout << -1 << endl;

	else

  		cout << n - taken << endl;

 	return 0;

}


// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

using namespace std;

main()

{

   int i,j,k,l;

    while(cin>>l){

  int x[l];

  for(i=0;i<l;i++)cin>>x[i];

  k=0;

  j=1; int a=0,b=0;

  for(i=0;i<l;i++){

    if(x[i]==j){  a++;  j++;  }

  }

  k=l-a;

  if(a==0) cout<<"-1"<<endl;

  else cout<<k<<endl;

    } return 0;



}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

const int N = 2e5 + 3;

int n, a[N];

vector<int> occ[N]; // occurrence vectors

int main() {

  	cin >> n;

  	for (int i = 0; i < n; ++i) {

      	cin >> a[i];

    	occ[a[i]].push_back(i);

    }

  	int prev = -1, taken = 0;

  	for (int val = 1; val <= n; ++val) {

  		bool found = false;

    	for (auto idx: occ[val]) {

        	if (idx > prev) {

             	prev = idx; 

              	taken++;

              	found = true;

             	break;

            }

        }

        if (!found)

        	break;

    }

  	if (taken == 0)

      	cout << -1 << endl;

	else

  		cout << n - taken << endl;

 	return 0;

}


// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

using namespace std;

int main()

{

    int n,i,j,k,l,x,c=0;

    cin>>n;

    int ar[n];

    for(i=0;i<n;i++)

    {

        cin>>ar[i];

        if(ar[i]==1)

        {

            c=1;

        }

    }

    if(c==0)

    {

        cout<<"-1"<<endl;

        return 0;

    }

    else

    {

        x=1;

        for(i=0;i<n;i++)

        {

            if(ar[i]==x)

                x++;

        }

        cout<<n-x+1<<endl;

    }

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <vector>



using namespace std;



int main(int argc, const char *argv[]) {

  int n;

  cin >> n;



  int cur = 1, ans = 0, found = false;

  for (int i = 0; i < n; i++) {

    int a;

    cin >> a;

    if (a == cur) {

      found = true;

      cur++;

    } else {

      ans++;

    }

  }



  cout << (found ? ans : -1) << '\n';



  return 0;

}

// Optimized solution to the same problem (faster):
#include <cstdio>



int main(int argc, const char *argv[]) {

  int n;

  scanf("%d", &n);



  int a[300000];

  for (int i = 0; i < n; i++) {

    scanf("%d", &a[i]);

  }



  int ans = 0, cur = 1;

  for (int i = 0; i < n; i++) {

    if (a[i] != cur) {

      ans++;

    } else {

      cur++;

    }

  }



  printf("%d\n", ans == n ? -1 : ans);

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <algorithm>

#include <cmath>

#include <iostream>

#include <string>

#include <vector>



using namespace std;



int main(int argc, const char *argv[]) {

  int n;

  cin >> n;

  vector<int> va(n);

  bool exists = false;

  for (auto &a : va) {

    cin >> a;

    if (a == 1) {

      exists = true;

    }

  }



  if (!exists) {

    cout << -1 << '\n';

    return 0;

  }



  int cur = 1, ans = 0;

  for (auto &a : va) {

    if (a != cur) {

      ans++;

    } else {

      cur++;

    }

  }



  cout << ans << '\n';

}

// Optimized solution to the same problem (faster):
#include <cstdio>



int main(int argc, const char *argv[]) {

  int n;

  scanf("%d", &n);



  int a[300000];

  for (int i = 0; i < n; i++) {

    scanf("%d", &a[i]);

  }



  int ans = 0, cur = 1;

  for (int i = 0; i < n; i++) {

    if (a[i] != cur) {

      ans++;

    } else {

      cur++;

    }

  }



  printf("%d\n", ans == n ? -1 : ans);

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>



using namespace std;



int main(int argc, const char *argv[]) {

  int n, a, cur = 1, ans = 0;

  cin >> n;

  for (int i = 0; i < n; i++) {

    cin >> a;

    if (a != cur) {

      ans++;

    } else {

      cur++;

    }

  }



  cout << (ans == n ? -1 : ans) << '\n';

}


// Optimized solution to the same problem (faster):
#include <cstdio>



int main(int argc, const char *argv[]) {

  int n;

  scanf("%d", &n);



  int a[300000];

  for (int i = 0; i < n; i++) {

    scanf("%d", &a[i]);

  }



  int ans = 0, cur = 1;

  for (int i = 0; i < n; i++) {

    if (a[i] != cur) {

      ans++;

    } else {

      cur++;

    }

  }



  printf("%d\n", ans == n ? -1 : ans);

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <vector>



using namespace std;



int main(int argc, const char *argv[]) {

  int n;

  cin >> n;

  vector<int> va(n);

  for (auto &a : va) {

    cin >> a;

  }



  int ans = 0, cur = 1;

  for (auto &a : va) {

    if (a != cur) {

      ans++;

    } else {

      cur++;

    }

  }



  cout << (ans == va.size() ? -1 : ans) << '\n';

}

// Optimized solution to the same problem (faster):
#include <cstdio>



int main(int argc, const char *argv[]) {

  int n;

  scanf("%d", &n);



  int a[300000];

  for (int i = 0; i < n; i++) {

    scanf("%d", &a[i]);

  }



  int ans = 0, cur = 1;

  for (int i = 0; i < n; i++) {

    if (a[i] != cur) {

      ans++;

    } else {

      cur++;

    }

  }



  printf("%d\n", ans == n ? -1 : ans);

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)

#define rep(i,n) REP(i,0,n)

#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)

#define ALLOF(c) (c).begin(), (c).end()

typedef long long ll;

typedef unsigned long long ull;



int dp[200005];



int main(){

  int N;

  cin >> N;

  vector<int> v;

  rep(i,N){

    int a;

    cin >> a;

    v.push_back(a);

  }



  rep(i,200005) dp[i] = -1;



  rep(i,N){

    if(dp[v[i]] != -1) continue;

    if(v[i] == 1){

      dp[1] = i;

    }else{

      if(dp[v[i]-1] != -1){

        dp[v[i]] = i;

      }

    }

  }



  int mx = -1;

  REP(i,1,N+1){

    if(dp[i] != -1) mx = max(mx, i);

  }



  if(mx == -1) cout << -1 << endl;

  else cout << N - mx << endl;

  

  return 0;

}




// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;

#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)

#define rep(i,n) REP(i,0,n)

#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)

#define ALLOF(c) (c).begin(), (c).end()

typedef long long ll;

typedef unsigned long long ull;





int main(){

  int N;

  cin >> N;

  vector<int> v;

  rep(i,N){

    int a;

    cin >> a;

    v.push_back(a);

  }



  int cnt = 0;

  rep(i,N){

    if(v[i] == cnt+1){

      cnt++;

    }

  }



  if(cnt == 0) cout << -1 << endl;

  else cout << N - cnt << endl;

  

  return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>



using namespace std;

int main(){

	ios::sync_with_stdio(0), cin.tie(0);

	int n, a, breaks=0, i=1, limit;

	cin >> n;

	limit = n - 1;

	while(n){

		cin >> a;

		if(a != i){

			if(breaks < limit){

				breaks++;

			}

			else{

				cout << -1 << '\n';

				return 0;

			}

		}

		else{

			i++;

		}

		n--;

	}

	cout << breaks << '\n';

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>



int main()

{

	int n; scanf("%d", &n);

	int now = 0;

	

	for(int i = 0; i < n; i++){

		int inp; scanf("%d", &inp);

		if(inp == now + 1) now++;

	}

	if(now == 0) printf("-1\n");

	else printf("%d\n", n - now);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;



int main()

{

    int n,f=1;

    cin>>n;

    int x[n];

    for(int i=0;i<n;i++)cin>>x[i];

    for(int i=0;i<n;i++)

    {

        if(x[i]==f)f++;



    }

    if(f==1)cout<<"-1"<<endl;

    else cout<<n-f+1<<endl;



    return 0;

}


// Optimized solution to the same problem (faster):
#include<cstdio>



int main()

{

	int n; scanf("%d", &n);

	int now = 0;

	

	for(int i = 0; i < n; i++){

		int inp; scanf("%d", &inp);

		if(inp == now + 1) now++;

	}

	if(now == 0) printf("-1\n");

	else printf("%d\n", n - now);

}

// Fastest known solution for this problem: