stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

using ll=long long;

const int N=1e5+10;

const int mod=1e9+7;

const int inf=1e9;



ll dp[N][2];

bool vis[N];

vector<int> g[N];



void dfs(int u){

    if(vis[u]) return;

    vis[u]=true;

    dp[u][0]=1;

    dp[u][1]=1;

    for(auto v : g[u]){

        if(vis[v]) continue;

        dfs(v);

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

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

    }

}



int main(){

    int n;

    cin>>n;

    int a,b;

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

        cin>>a>>b;

        g[a].push_back(b);

        g[b].push_back(a);

    }

    dfs(1);

    ll ans=(dp[1][0] + dp[1][1])%mod;

    cout<<ans<<'\n';

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 3.444450387499147x):
#include <bits/stdc++.h>

#define f first

#define s second

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

#define pb push_back

#define all(s) begin(s), end(s)

#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

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

#define ENDL '\n'

using namespace std;

typedef long double ld;

typedef long long lli;

typedef pair<lli,lli> ii;

typedef vector<int> vi;

typedef vector<lli> vl;

typedef vector<ii> vii;

typedef vector<vi> vvi;

typedef vector<vl> vvl;

#define deb(x) cout << #x": " << (x) << endl;



const int N = 3e3+5;

const lli INF = 1e18;

const lli MOD = 1e9+7;



vi m;

vi primes(1,2);

vector<vector<ld>>nk;

void sieve(){

	m = vi(N+1,0);

	for(int i = 4; i<N; i+=2)m[i]=1;

	for(int i = 3; i*i<=N; i+=2)if(!m[i])for(int j = i*i; j<N; j+=i)m[j]=1;

	for(int i = 3; i<N; i+=2)if(!m[i])primes.pb(i);

}



void pascal(){

	nk =vector<vector<ld>>(N,vector<ld>(N,0.0));

	fore(i,0,N)nk[i][0]=nk[i][i]=1.0;

	fore(i,1,N)fore(j,1,i)nk[i][j]=nk[i-1][j-1]+nk[i-1][j];

}



lli gcd(lli a, lli b){return (b?gcd(b,a%b):a);}



lli lcm(lli a, lli b){

	if(a<b)swap(a,b);

	lli c = gcd(a,b);

	a/=c;

	return a*b;

}



int popcount(lli x) { return __builtin_popcountll(x); }



lli poww(lli a, lli b){

	lli res =1;

	while(b){

		if(b&1) res = res * a;

		a = a*a;

		b/=2;

	}

	return res;

}



lli powm(lli a, lli b){

	lli res =1;

	while(b){

		if(b&1) res = (res * a)%MOD;

		a = (a*a)%MOD;

		b/=2;

	}

	return res;

}



bool isPrime(lli x){

	if(x==1)return 0;

	for(auto i:primes){

		if(i*i>x)return 1;

		if(x%i==0)return 0;

	}

	return 1;

}



lli SUM(lli a, lli b, lli mod=MOD){

	return (a+b+mod)%mod;

}

lli PROD(lli a, lli b, lli mod=MOD){

	a%=mod; b%=mod;

	return a*b % mod;

}

int dx[4]={-1,0,0,1}, dy[4]={0,1,-1,0};



// ---- コーディングはここから！ ('-')7



void solve(){

	int n; cin>>n;

	vvi v(n+1);

	fore(i,0,n-1){

		int a,b; cin>>a>>b;

		v[a].pb(b);

		v[b].pb(a);

	}

	if(n==1){cout<<2<<ENDL;return;}

	vvl dp(n+1,vl(2,1));

	auto f=[&](auto f, int u, int pr)->void{

		for(auto i:v[u])if(i!=pr)f(f,i,u);

		for(auto i:v[u])if(i!=pr)dp[u][1]=PROD(dp[u][1],SUM(dp[i][0],dp[i][1]));

		for(auto i:v[u])if(i!=pr)dp[u][0]=PROD(dp[u][0],dp[i][1]);

	};

	f(f,1,1);



	cout<<SUM(dp[1][0],dp[1][1])<<ENDL;

}



int main(){_

	//int t; cin>>t; while(t--)

	solve();

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 25, M = 1e6 + 25, oo = 1e9+7;

const long long OO = 1e18+25;

#define par pair<pair<int,int>, int>

#define f first

#define s second



int n, mem[N][2], u,v,vis[N][2];

vector <int> g[N];



long long dp(int ele=1, int precol=0)

{

    int &ret= mem[ele][precol];

    if(vis[ele][precol] && ret == -1) return 1;

    if(~ret) return ret;

    vis[ele][precol] = 1;

    int ans=0, ans1=1;

    if(!precol) ans =1;

    for(int e : g[ele])

    {

        vis[ele][1] ^= 2;

        ans1 = (1ll*ans1*dp(e, 0))%oo;

        vis[ele][1] ^= 2;

        if(!precol) ans = (1ll*ans*dp(e, 1))%oo;

    }

    ret = (ans+ans1)%oo;

    return ret;

}



int main()

{

    memset(mem, -1, sizeof mem);

    scanf("%d",&n);

    for(int i=0; i<n-1; i++) {scanf("%d%d",&u,&v);g[u].push_back(v);g[v].push_back(u);}

    printf("%lld",dp());

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.240540770595815x):
#include<bits/stdc++.h>

#define REP(x,y,z) for(int x=y;x<=z;x++)

#define MSET(x,y) memset(x,y,sizeof(x))

#define M 100005

#define MOD 1000000007

using namespace std;

using LL = long long;

int n;

LL dp[M][2];

vector<int> e[M];

void dfs(int cur,int fa) {

    dp[cur][0] = dp[cur][1] = 1;

    for (int i: e[cur]) if (i!=fa) {

        dfs(i, cur);

        dp[cur][0] *= (dp[i][0] + dp[i][1]);

        dp[cur][0] %= MOD;

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

    }

}

int main()

{

    scanf("%d", &n);

    REP(i,1,n-1) {

        int x, y;

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

        e[x].push_back(y);

        e[y].push_back(x);

    }



    dfs(1, -1);

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

    return 0;

}
// Slower C++ solution:
//       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;

	

	

}


// Faster C++ solution for the same problem (measured speedup: 1.8018944341746093x):
#include<bits/stdc++.h>

#define REP(x,y,z) for(int x=y;x<=z;x++)

#define MSET(x,y) memset(x,y,sizeof(x))

#define M 100005

#define MOD 1000000007

using namespace std;

using LL = long long;

int n;

LL dp[M][2];

vector<int> e[M];

void dfs(int cur,int fa) {

    dp[cur][0] = dp[cur][1] = 1;

    for (int i: e[cur]) if (i!=fa) {

        dfs(i, cur);

        dp[cur][0] *= (dp[i][0] + dp[i][1]);

        dp[cur][0] %= MOD;

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

    }

}

int main()

{

    scanf("%d", &n);

    REP(i,1,n-1) {

        int x, y;

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

        e[x].push_back(y);

        e[y].push_back(x);

    }



    dfs(1, -1);

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

    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 25, M = 1e6 + 25, oo = 1e9+7;

const long long OO = 1e18+25;

#define par pair<pair<int,int>, int>

#define f first

#define s second



int n, mem[N][2], u,v,vis[N][2];

vector <int> g[N];



long long dp(int ele=1, int precol=0)

{

    int &ret= mem[ele][precol];

    if(vis[ele][precol] && ret == -1) return 1;

    if(~ret) return ret;

    vis[ele][precol] = 1;

    int ans=0, ans1=1;

    if(!precol) ans =1;

    for(int e : g[ele])

    {

        vis[ele][1] ^= 2;

        ans1 = (1ll*ans1*dp(e, 0))%oo;

        vis[ele][1] ^= 2;

        if(!precol) ans = (1ll*ans*dp(e, 1))%oo;

    }

    ret = (ans+ans1)%oo;

    return ret;

}



int main()

{

    memset(mem, -1, sizeof mem);

    scanf("%d",&n);

    for(int i=0; i<n-1; i++) {scanf("%d%d",&u,&v);g[u].push_back(v);g[v].push_back(u);}

    printf("%lld",dp());

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.2350283404359415x):
/*Largest Independent Set Problem



Definindo que dp[atual][0] é o máximo do vértice PRETO atual e

que dp[atual][1] é o máximo do vértice BRANCO atual (como exercício).



Percorrer os vértices recursivamente até a raiz e ir calculando

o máximo permitido no PRETO e BRANCO. O resultado final será

a soma do máximo deles.



Restrições:



PRETO: todos os seus filhos (ou vértices ligados) precisam ser BRANCOS! (evento dependente)

BRANCO: a cor de qualquer um dos seus filhos (vértices) NÃO IMPORTA! (evento independente)



*Eventos Dependentes: multiplicação

*Eventos Independentes: soma e multiplicação

*/



#include <bits/stdc++.h>



#define MAX 100003

#define MODULO 1000000007



using namespace std;



vector<int> grafo[MAX];

long long dp[MAX][2]; //valores das colunas -> 0 - PRETO e 1 - BRANCO 

long N;



//Semelhante ao conceito de DFS: marcar os visitados na matriz e percorrer recursivamente

void LIS(int vAtual, int vPosterior){



    dp[vAtual][0] = dp[vAtual][1] = 1; //Visitado



    //Percorrendo o grafo

    for(int i = 0; i < grafo[vAtual].size(); i++){

        int elemento = grafo[vAtual][i];

        

        if(elemento != vPosterior){

            //DFS

            LIS(elemento, vAtual);



            //PRETO

            dp[vAtual][0] = dp[vAtual][0] * dp[elemento][1] % MODULO;



            //BRANCO

            dp[vAtual][1] = dp[vAtual][1] * ((dp[elemento][0] + dp[elemento][1]) % MODULO) % MODULO;

        }

    }

}



int main(){



    cin >> N;



    for(long i = 1; i < N; i++){

        long valor1, valor2;

        cin >> valor1 >> valor2;



        grafo[valor1].push_back(valor2);

        grafo[valor2].push_back(valor1);

    }



    //Vértice de Início = 1

    LIS(1, 0);



    //Os resultados são armazenados nas primeiras posições do grafo (recursão)

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

    

    return 0;

}
// Slower C++ solution:
//       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;

	

	

}


// Faster C++ solution for the same problem (measured speedup: 1.7938875895312996x):
/*Largest Independent Set Problem



Definindo que dp[atual][0] é o máximo do vértice PRETO atual e

que dp[atual][1] é o máximo do vértice BRANCO atual (como exercício).



Percorrer os vértices recursivamente até a raiz e ir calculando

o máximo permitido no PRETO e BRANCO. O resultado final será

a soma do máximo deles.



Restrições:



PRETO: todos os seus filhos (ou vértices ligados) precisam ser BRANCOS! (evento dependente)

BRANCO: a cor de qualquer um dos seus filhos (vértices) NÃO IMPORTA! (evento independente)



*Eventos Dependentes: multiplicação

*Eventos Independentes: soma e multiplicação

*/



#include <bits/stdc++.h>



#define MAX 100003

#define MODULO 1000000007



using namespace std;



vector<int> grafo[MAX];

long long dp[MAX][2]; //valores das colunas -> 0 - PRETO e 1 - BRANCO 

long N;



//Semelhante ao conceito de DFS: marcar os visitados na matriz e percorrer recursivamente

void LIS(int vAtual, int vPosterior){



    dp[vAtual][0] = dp[vAtual][1] = 1; //Visitado



    //Percorrendo o grafo

    for(int i = 0; i < grafo[vAtual].size(); i++){

        int elemento = grafo[vAtual][i];

        

        if(elemento != vPosterior){

            //DFS

            LIS(elemento, vAtual);



            //PRETO

            dp[vAtual][0] = dp[vAtual][0] * dp[elemento][1] % MODULO;



            //BRANCO

            dp[vAtual][1] = dp[vAtual][1] * ((dp[elemento][0] + dp[elemento][1]) % MODULO) % MODULO;

        }

    }

}



int main(){



    cin >> N;



    for(long i = 1; i < N; i++){

        long valor1, valor2;

        cin >> valor1 >> valor2;



        grafo[valor1].push_back(valor2);

        grafo[valor2].push_back(valor1);

    }



    //Vértice de Início = 1

    LIS(1, 0);



    //Os resultados são armazenados nas primeiras posições do grafo (recursão)

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

    

    return 0;

}
// Slower C++ solution:
//       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;

	

	

}


// Faster C++ solution for the same problem (measured speedup: 1.4392012128318161x):
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const int mx = 1e5+5;

const int md = 1e9+7;



int dp[mx][2];

int n, vis[mx];

vector<int> g[mx];



int fun(int u, int c){

    int &ans = dp[u][c];

    if(ans != -1) return ans;



    vis[u] = 1; ans = 1;

    for(int v : g[u])

    if(!vis[v]){

        long long res = 0;

        res += fun(v, 0); res %= md;

        if(c == 0)

            res += fun(v, 1), res %= md;

        ans = (ans * res) % md;

    }vis[u] = 0; return ans;

}



int main(){



    cin.tie( nullptr );

    ios_base::sync_with_stdio(0);



    cin >> n;

    memset(dp, -1, sizeof dp);

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

        cin >> u >> v;

        g[u].push_back(v);

        g[v].push_back(u);

    }cout << (fun(1,0)+fun(1,1))%md << "\n";



    return 0;

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 1.5059217427300569x):
#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;

}
// Slower C++ solution:
#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;

}


// Faster C++ solution for the same problem (measured speedup: 6.476408887663314x):
#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;



}
// Slower C++ solution:
#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;

}


// Faster C++ solution for the same problem (measured speedup: 6.410103943229018x):
#include<bits/stdc++.h>

using namespace std;

int main()

{

 int N,i,j=-1,Y,cnt=0;

    map<int,int>m;

  cin>>N;

           

      for(i=0;i<N;i++){cin>>m[i];}

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

          if(m[i]==1){j=i;break;}

      }

    int x=m[j];

    if(j!=-1){cnt=1;

    for(i=j+1;i<N;i++){

       if(m[i]-m[j]==x){cnt++;x++;}

    }

        }

    if(j==-1){cout<<"-1\n";}

    else{cout<<N-cnt<<endl;}

    

    



     

 

    

    

    



    }
// Slower C++ solution:
#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;

}


// Faster C++ solution for the same problem (measured speedup: 6.404048386088519x):
#include <iostream>

#include<algorithm>

#include<string>

#include<map>

#include<math.h>

#include<string.h>

#include<assert.h>

#include<queue>

using namespace std;

#define endl '\n'

#define pb push_back

#define mp make_pair

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

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

#define mem0(s) memset(s,0,sizeof(s))

#define meminf(s) memset(s,0x3f,sizeof(s))

#define mp_insert(a,b,c) mp.insert(map<pair<int,int>,int>::value_type(make_pair(a,b),c))

typedef long long ll;

typedef unsigned long long ull;

inline ll lowbit(ll x){return x&(-x);}

inline ll min(ll a,ll b){return a<b?a:b;}

inline ll max(ll a,ll b){return a>b?a:b;}

const int INF=0x3f3f3f3f,max_n=200005,mod=19260817;

inline ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}

inline ll pow(ll a,ll b,ll mod){ll res=1;a%=mod;assert(b>=0);for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}

int max1=-1;

int n;

int a[max_n];

int f(int x)

{

	int count1=2;

	int ans=1;

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

	{

		if(a[i]==count1)

		{

			ans++;

			count1++;

		}

	}

	return ans;

}

int main()

{

	std::ios::sync_with_stdio(false);

	std::cin.tie(0);

	cin>>n;

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

	{

		cin>>a[i];

	}

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

	{

		if(max1>=n-i+1)

		{

			break;

		}

		if(a[i]==1)

		{

			max1=max(max1,f(i));

			break;

		}

	}

	if(max1==-1)

	cout<<"-1";

	else

	cout<<n-max1;

}
// Slower C++ solution:
#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;

}


// Faster C++ solution for the same problem (measured speedup: 6.4279655364128585x):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main() {

	

 ll n;

 cin>>n;

 ll a[n+1];

 ll k=1;

 std::vector<ll>v ;

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

 {

     cin>>a[i];

     if(a[i]==k)

     {

         v.push_back(k);

         k++;

     }

 }

 if(v.size()==0)

 {

     cout<<"-1\n";

 }

 else

 {

     cout<<n-v.size()<<"\n";

 }

}