// organize and work - Abhinav IIITA
		#include <bits/stdc++.h>
		//#include <boost/algorithm/string.hpp> 
		#include<random>
		#include<time.h>
		#define ff first
		#define ss second
		using namespace std;
		#define MP make_pair
		#define PB push_back
		#define ll long long
		#define int long long
		#define f(i,x,n) for(int i=x;i<n;i++)
		#define rf(i,x,n) for(int i=x;i>=n;--i)
		#define ld long double
		#define mod 1000000007
		int adj[1005][1005];
		int zrr[1000][1000];
		int zpp[200005];
		int prime[3000005];
		int fact[1000005];
		void seive(){
			for(int i=1;i<1000005;++i)
			prime[i]=1;
			for(int i=2;i<1000005;++i){
				prime[i]++;
				for(int j=2*i;j<1000005;j+=i)
				prime[j]++;
			}
		}
		int modinv(int a,int b){
			int res=1;
			while(b!=0){
				if(b&1){
					res*=a;
					res%=mod;
				}
				a*=a;
				a%=mod;
				b/=2;
			}
			return res;
		}
		void factorial(){
			fact[0]=1;
			for(int i=1;i<1000005;++i)
			{fact[i]=i*fact[i-1];fact[i]%=mod;}
		}
		void set_bit(int a){
			int pp=0;
			while(a!=0){
				pp+=a%2;
				a/=2;
			}
			cout<<pp<<endl;
		}
		int Stoi(string s){
			int ap=0;
			int tt=1;
			for(int i=s.size()-1;i>=0;i--){
				int xp=s[i]-'0';
				ap+=xp*tt;
				tt*=2;
			}
			return ap;
		}
		void print(int arr[],int n){
		  for(int i=0;i<n;++i)
			cout<<arr[i]<<" ";
		  cout<<endl;
		}
		int min_cost(int n){
		  int cost[n][n];
		  for(int i=0;i<n;++i)
			for(int j=0;j<n;++j)
			  cost[i][j]=LLONG_MAX;
		  cost[0][0]=zrr[0][0];
		  queue<pair<int,int>> q;
		  q.push(make_pair(0,0));
		  while(!q.empty()){
			pair<int,int> pp=q.front();q.pop();
			int x,y;x=pp.first;y=pp.second;
			int ap[4][4];
			ap[0][0]=x-1;ap[0][1]=y;
			ap[1][0]=x+1;ap[1][1]=y;
			ap[2][0]=x;ap[2][1]=y-1;
			ap[3][0]=x;ap[3][1]=y+1;
			f(i,0,4){
			  if((ap[i][0]>=0 && ap[i][0]<n) && (ap[i][1]>=0 && ap[i][1]<n)){
				int temp=cost[x][y]+zrr[ap[i][0]][ap[i][1]];
				if(temp<cost[ap[i][0]][ap[i][1]]){
				  cost[ap[i][0]][ap[i][1]]=temp;
				  q.push(make_pair(ap[i][0],ap[i][1]));
				}
			  }
			}
		  }
		  return cost[n-1][n-1];
		}
		void merge(int arr[],int brr[], int l, int m, int r) 
		{ 
		  int i, j, k; 
		  int n1 = m - l + 1; 
		  int n2 = r - m; 
		  int L[n1], R[n2]; 
		  int l1[n1] ,r1[n2];
		  for (i = 0; i < n1; i++) 
			{L[i] = arr[l + i];l1[i]=brr[l+i];} 
		  for (j = 0; j < n2; j++) 
			{R[j] = arr[m + 1+ j];r1[j]=brr[m+1+j];} 
		  i = 0;  
		  j = 0; 
		  k = l;  
		  while (i < n1 && j < n2) 
		  { 
			if (L[i] <= R[j]) 
			{ 
			  arr[k] = L[i];
			  brr[k]=l1[i]; 
			  i++; 
			} 
			else
			{ 
			  arr[k] = R[j];
			  brr[k]=r1[j]; 
			  j++; 
			} 
			k++; 
		  } 
		  while (i < n1) 
		  { 
			arr[k] = L[i]; 
			brr[k]=l1[i];
			i++; 
			k++; 
		  } 
		  while (j < n2) 
		  { 
			arr[k] = R[j];
			brr[k]=r1[j]; 
			j++; 
			k++; 
		  } 
		} 
		void mergeSort(int arr[],int brr[], int l, int r) 
		{ 
		  if (l < r) 
		  { 
			int m = l+(r-l)/2; 
			mergeSort(arr,brr, l, m); 
			mergeSort(arr,brr, m+1, r); 
			merge(arr,brr, l, m, r); 
		  } 
		}
		void sopp(int arr[],int brr[],int n){
			int s=0;
			for(int i=1;i<n;++i){
				if(arr[i]!=arr[i-1])
				{
					sort(arr+s,arr+i);
					s=i;
				}
			}
			sort(arr+s,arr+n);
		}
		int func(int p){
			p*=2;
			int l=0;
			int pp=l*(l+1);
			while(true){
				if(pp>p){
					l--;
					pp=l*(l+1);
					p-=pp;
					return l+p;
				}
				l++;
				pp=l*(l+1);
			}
		}
		int setBitNumber(int n) 
		{ 
			if (n == 0) 
				return 0; 

			int msb = 0; 
			while (n != 0) { 
				n = n / 2; 
				msb++; 
			} 
			msb--;
			return (1 << msb); 
		} 
		int setBit(int n) 
		{ 
			if (n == 0) 
				return 0; 

			int msb = 0; 
			while (n != 0) { 
				n = n / 2; 
				msb++; 
			} 
			msb--;
			return msb; 
		} 
		int funk(int a,int b,int m){
			if(b>=a)
			return b-a;
			return b-a+m;			
		}
		void solve(){
				int n;cin>>n;
				map<int,vector<int>> mp;
				for(int i=0;i<n;++i){
					int x;cin>>x;
					mp[x].push_back(i);
				}		
				for(auto it : mp)
				sort(it.second.begin(),it.second.end());
				int num=1; int pre;
				int ans=0;
				for(auto it : mp){
					if(it.first!=num && num==1){
						cout<<"-1";
						return ;
					}
					if(it.first!=num)
					break;
					if(num==1){
						pre=(it.second)[0];
						ans=pre;
					} else {
						int fg=0;
						for(auto ip : it.second) {
							if(ip>pre){
								fg=1;
								ans+=ip-pre-1;
								pre=ip;
								break;
							}
						}
						if(!fg)
						break;
					}
					num++;
				}
				ans+=n-1-pre;
				cout<<ans;
		}
		int32_t main()
		{
		  ios_base::sync_with_stdio(false);
		  cin.tie(NULL);
		  int t;//cin>>t;
		  t=1;
		  while(t--)//
		  solve();
		  return 0;
		}


Measured memory_bytes: 22144