#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count __gcd reverse etc.
#include <cstdlib>	// require abs exit
#include <cstdio>	// require scanf printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream
#include <cstring>	// require memset
#include <cctype>	// require tolower, toupper
#include <fstream>	// require freopen
#include <ctime>
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()
#define DEBUG 0

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
string FILE_NAME = "testcase.C";
string NAME;
string itos (int n )
{
	stringstream ss;
	ss << n;

	return ss.str();
}

int main()
{
//	cut here before submit
#if DEBUG
	NAME = FILE_NAME;
	int CNT = 1;
	NAME += itos (CNT );
	while (freopen (NAME.c_str() , "r", stdin ) != NULL ) {
#endif
	int n;
	cin >> n;
	map<int,int> cnt; cnt.clear();
	rep (i, n ){
		int a;
		cin >> a;
		cnt[a]++;
	} // end rep
//	cerr << "sum: " << sum << endl;
	while (true ){
		bool found = false;
		map<int,int>::iterator it = cnt.begin();
		for (; it != cnt.end(); it++ ){
			int an = (*it).first;
			int cn = (*it).second;
			if (cn >= 2 ){
				cnt[an]   -= (cn-1);
				if (cnt.count (an+1 ) )
					cnt[an+1] += (cn-1)/2;
				else
					cnt[an+1] = (cn-1)/2;
				found |= true;
			} // end if
		} // end for
		if (!found ) break;
	} // end while

	int max_a = 0;
	vector<int> arr; arr.clear();
	for (map<int,int>::iterator it = cnt.begin(); it != cnt.end(); it++ ){
		int ca = (*it).first;
		arr.push_back (ca );
		max_a = ca;
	} // end for
	cout << max_a - arr.size() + 1 << endl;
#if DEBUG
	CNT++;	// cut here before submit
	NAME = FILE_NAME;
	NAME += itos (CNT );
	} // end loop; cut here before submit
#endif
	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count __gcd reverse etc.
#include <cstdlib>	// require abs exit
#include <cstdio>	// require scanf printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream
#include <cstring>	// require memset
#include <cctype>	// require tolower, toupper
#include <fstream>	// require freopen
#include <ctime>
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()
#define DEBUG 0

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
string FILE_NAME = "testcase.C";
string NAME;
string itos (int n )
{
	stringstream ss;
	ss << n;

	return ss.str();
}

int main()
{
//	cut here before submit
#if DEBUG
	NAME = FILE_NAME;
	int CNT = 1;
	NAME += itos (CNT );
	while (freopen (NAME.c_str() , "r", stdin ) != NULL ) {
#endif
	int n;
	cin >> n;
	map<int,int> cnt; cnt.clear();
	rep (i, n ){
		int a;
		cin >> a;
		cnt[a]++;
	} // end rep
//	cerr << "sum: " << sum << endl;
	while (true ){
		bool found = false;
		map<int,int>::iterator it = cnt.begin();
		for (; it != cnt.end(); it++ ){
			int an = (*it).first;
			int cn = (*it).second;
			if (cn >= 2 ){
				cnt[an]   -= cn;
				if (cnt.count (an+1 ) )
					cnt[an+1] += cn/2;
				else
					cnt[an+1] = cn/2;
				found |= true;
			} // end if
		} // end for
		if (!found ) break;
	} // end while

	int max_a = 0;
	vector<int> arr; arr.clear();
	for (map<int,int>::iterator it = cnt.begin(); it != cnt.end(); it++ ){
		int ca = (*it).first;
		arr.push_back (ca );
		max_a = ca;
	} // end for
	cout << max_a - arr.size() + 1 << endl;
#if DEBUG
	CNT++;	// cut here before submit
	NAME = FILE_NAME;
	NAME += itos (CNT );
	} // end loop; cut here before submit
#endif
	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count __gcd reverse etc.
#include <cstdlib>	// require abs exit
#include <cstdio>	// require scanf printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream
#include <cstring>	// require memset
#include <cctype>	// require tolower, toupper
#include <fstream>	// require freopen
#include <ctime>
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()
#define DEBUG 0

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
string FILE_NAME = "testcase.C";
string NAME;
string itos (int n )
{
	stringstream ss;
	ss << n;

	return ss.str();
}

int main()
{
//	cut here before submit
#if DEBUG
	NAME = FILE_NAME;
	int CNT = 1;
	NAME += itos (CNT );
	while (freopen (NAME.c_str() , "r", stdin ) != NULL ) {
#endif
	int n;
	cin >> n;
	map<int,int> cnt; cnt.clear();
	rep (i, n ){
		int a;
		cin >> a;
		cnt[a]++;
	} // end rep
//	cerr << "sum: " << sum << endl;
	map<int,int>::iterator it = cnt.begin();
	for (; it != cnt.end(); it++ ){
		int an = (*it).first;
		int cn = (*it).second;
		if (cn >= 2 ){
			cnt[an]   -= cn;
			cnt[an+1] += cn/2;
		} // end if
	} // end for
	int max_a = 0;
	vector<int> arr; arr.clear();
	for (it = cnt.begin(); it != cnt.end(); it++ ){
		int ca = (*it).first;
		arr.push_back (ca );
		max_a = ca;
	} // end for
	cout << max_a - arr.size() + 1 << endl;
#if DEBUG
	CNT++;	// cut here before submit
	NAME = FILE_NAME;
	NAME += itos (CNT );
	} // end loop; cut here before submit
#endif
	return 0;
}


// IN the name of Allah
/*
ID: ahmed.m4
PROG:
LANG: C++
*/
//s/stdc++.h>
#include <string.h>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <utility>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <limits.h>
#include <string>
#define PI 3.14159265
#define cell(i,j) ((i)*m+(j))
using namespace std;
int gcd(int a,int b){return b==0?a:gcd(b,a%b);}
int setbit(int n,int idx){return n|1<<idx;}
int countbits(int n){int ret=0;while(n){if(n&1)ret++;n/=2;};return ret;}
int getbit(int n,int idx){return ((n >> idx) & 1) ==1 ;}
typedef pair<int,int>ii;
typedef pair<int,pair<int,int> >iii;
typedef bitset<20>mask;
const long long oo=1000000009;
/*struct node{
bool operator < (node n) const{
return d < n.d;
}
};*/
map<long long,int>mp;
set<long long>s;
long long mx=0,a;
int main()
{
  //freopen("test.in","r",stdin);
  int n;
  cin>>n;
  for(int i=0;i<n;i++)cin>>a,mx=max(a,mx),mp[a]++,s.insert(a);
  set<long long>::iterator it;
  for(it=s.begin();it!=s.end();it++){
    int t=*(it);//cout<<t<<mp[t]<<' ';
    if(mp[t]>2){
        mp[(*it)+1]+=((t-1)/2);
        mx=max(mx,*(it)+1);
        s.insert((*it)+1);
    }
  }
  cout<<mx+1-s.size();
  return 0;
}


// IN the name of Allah
/*
ID: ahmed.m4
PROG:
LANG: C++
*/
//s/stdc++.h>
#include <string.h>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <utility>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <limits.h>
#include <string>
#define PI 3.14159265
#define cell(i,j) ((i)*m+(j))
using namespace std;
int gcd(int a,int b){return b==0?a:gcd(b,a%b);}
int setbit(int n,int idx){return n|1<<idx;}
int countbits(int n){int ret=0;while(n){if(n&1)ret++;n/=2;};return ret;}
int getbit(int n,int idx){return ((n >> idx) & 1) ==1 ;}
typedef pair<int,int>ii;
typedef pair<int,pair<int,int> >iii;
typedef bitset<20>mask;
const long long oo=1000000009;
/*struct node{
bool operator < (node n) const{
return d < n.d;
}
};*/
map<long long,int>mp;
long long mx=0,a,last,c;
int main()
{
  //freopen("test.in","r",stdin);
  int n;
  cin>>n;
  vector<long long>v;
  for(int i=0;i<n;i++){
    cin>>a;
    mp[a]++;
    if(mp[a]==1)v.push_back(a);
  }
  int idx=1;
  queue<long long>q;
  q.push(v[0]);
  while(!q.empty()){
   long long next,m,x=q.front();q.pop();
   c++;
   mx=max(mx,x);
   if(mp[x]>=2){
   next=x+1,m=(mp[x])/2;
   if(m%2==0)c--;
   mp[x+1]+=m;
   if(idx<v.size()&&next<v[idx])
   q.push(next);
   else if(next==v[idx]&&idx<v.size())q.push(next),idx++;
   else if(idx>=v.size())q.push(next);
   }
   else{
    if(idx<v.size())
    q.push(v[idx]);idx++;
   }
  }
  cout<<mx+1-c;
  return 0;
}


// IN the name of Allah
/*
ID: ahmed.m4
PROG:
LANG: C++
*/
//s/stdc++.h>
#include <string.h>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <utility>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <limits.h>
#include <string>
#define PI 3.14159265
#define cell(i,j) ((i)*m+(j))
using namespace std;
int gcd(int a,int b){return b==0?a:gcd(b,a%b);}
int setbit(int n,int idx){return n|1<<idx;}
int countbits(int n){int ret=0;while(n){if(n&1)ret++;n/=2;};return ret;}
int getbit(int n,int idx){return ((n >> idx) & 1) ==1 ;}
typedef pair<int,int>ii;
typedef pair<int,pair<int,int> >iii;
typedef bitset<20>mask;
const long long oo=1000000009;
/*struct node{
bool operator < (node n) const{
return d < n.d;
}
};*/
map<long long,int>mp;
long long mx=0,a,last,c;
int main()
{
  //freopen("test.in","r",stdin);
  int n;
  cin>>n;
  vector<long long>v;
  for(int i=0;i<n;i++){
    cin>>a;
    mp[a]++;
    if(mp[a]==1)v.push_back(a);
  }
  int idx=1;
  queue<long long>q;
  q.push(v[0]);
  while(!q.empty()){
   long long next,m,x=q.front();q.pop();
   c++;
   mx=max(mx,x);
   if(mp[x]>=2){
   next=x+1,m=(mp[x])/2;
   if(mp[x]%2)c--;
   mp[x+1]+=m;
   if(idx<v.size()&&next<v[idx])
   q.push(next);
   else if(next==v[idx]&&idx<v.size())q.push(next),idx++;
   else if(idx>=v.size())q.push(next);
   }
   else{
    if(idx<v.size())
    q.push(v[idx]);idx++;
   }
  }
  cout<<mx+1-c;
  return 0;
}


// IN the name of Allah
/*
ID: ahmed.m4
PROG:
LANG: C++
*/
//s/stdc++.h>
#include <string.h>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <utility>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <limits.h>
#include <string>
#define PI 3.14159265
#define cell(i,j) ((i)*m+(j))
using namespace std;
int gcd(int a,int b){return b==0?a:gcd(b,a%b);}
int setbit(int n,int idx){return n|1<<idx;}
int countbits(int n){int ret=0;while(n){if(n&1)ret++;n/=2;};return ret;}
int getbit(int n,int idx){return ((n >> idx) & 1) ==1 ;}
typedef pair<int,int>ii;
typedef pair<int,pair<int,int> >iii;
typedef bitset<20>mask;
const long long oo=1000000009;
/*struct node{
bool operator < (node n) const{
return d < n.d;
}
};*/
map<long long,int>mp;
set<long long>s;
long long mx=0,a;
int main()
{
  //freopen("test.in","r",stdin);
  int n;
  cin>>n;
  for(int i=0;i<n;i++){cin>>a,mx=max(a,mx),mp[a]++;
  if(mp[a]==1)
  s.insert(a);
  }
  set<long long>::iterator it;
  for(it=s.begin();it!=s.end();it++){
    int t=*(it);//cout<<t<<mp[t]<<' ';
    if(mp[t]>2){
        mp[(*it)+1]+=((t-1)/2);
        mx=max(mx,*(it)+1);
        s.insert((*it)+1);
    }
  }
  cout<<mx+1-s.size();
  return 0;
}


// IN the name of Allah
/*
ID: ahmed.m4
PROG:
LANG: C++
*/
//s/stdc++.h>
#include <string.h>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <utility>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <limits.h>
#include <string>
#define PI 3.14159265
#define cell(i,j) ((i)*m+(j))
using namespace std;
int gcd(int a,int b){return b==0?a:gcd(b,a%b);}
int setbit(int n,int idx){return n|1<<idx;}
int countbits(int n){int ret=0;while(n){if(n&1)ret++;n/=2;};return ret;}
int getbit(int n,int idx){return ((n >> idx) & 1) ==1 ;}
typedef pair<int,int>ii;
typedef pair<int,pair<int,int> >iii;
typedef bitset<20>mask;
const long long oo=1000000009;
/*struct node{
bool operator < (node n) const{
return d < n.d;
}
};*/
map<long long,int>mp;
long long mx=0,a,last,c;
int main()
{
  //freopen("test.in","r",stdin);
  int n;
  cin>>n;
  vector<long long>v;
  for(int i=0;i<n;i++){
    cin>>a;
    mp[a]++;
    if(mp[a]==1)v.push_back(a);
  }
  int idx=1;
  queue<long long>q;
  q.push(v[0]);
  while(!q.empty()){
   long long next,m,x=q.front();q.pop();
   c++;
   mx=max(mx,x);
   if(mp[x]>=2){
   next=x+1,m=(mp[x])/2;
   if((mp[x]%2)==0)c--;
   mp[x+1]+=m;
   if(idx<v.size()&&next<v[idx])
   q.push(next);
   else if(next==v[idx]&&idx<v.size())q.push(next),idx++;
   else if(idx>=v.size())q.push(next);
   }
   else{
    if(idx<v.size())
    q.push(v[idx]);idx++;
   }
  }
  cout<<mx+1-c;
  return 0;
}


// IN the name of Allah
/*
ID: ahmed.m4
PROG:
LANG: C++
*/
//s/stdc++.h>
#include <string.h>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <utility>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <limits.h>
#include <string>
#define PI 3.14159265
#define cell(i,j) ((i)*m+(j))
using namespace std;
int gcd(int a,int b){return b==0?a:gcd(b,a%b);}
int setbit(int n,int idx){return n|1<<idx;}
int countbits(int n){int ret=0;while(n){if(n&1)ret++;n/=2;};return ret;}
int getbit(int n,int idx){return ((n >> idx) & 1) ==1 ;}
typedef pair<int,int>ii;
typedef pair<int,pair<int,int> >iii;
typedef bitset<20>mask;
const long long oo=1000000009;
/*struct node{
bool operator < (node n) const{
return d < n.d;
}
};*/
map<long long,int>mp;
long long mx=0,a,last,c;
int main()
{
  //freopen("test.in","r",stdin);
  int n;
  cin>>n;
  vector<long long>v;
  for(int i=0;i<n;i++){
    cin>>a;
    mp[a]++;
    if(mp[a]==1)v.push_back(a);
  }
  int idx=1;
  queue<long long>q;
  q.push(v[0]);
  while(!q.empty()){
  long long next,m,x=q.front();q.pop();
   c++;
   mx=max(mx,x);
   if(mp[x]>2){
   next=x+1,m=(mp[x]-1)/2;
   if(idx<v.size()&&next<v[idx])
   q.push(next);
   else if(next==v[idx])q.push(next),idx++;
   }
   else{
    if(idx<v.size())
    q.push(v[idx]);idx++;
   }
  }
  cout<<mx+1-c;
  return 0;
}


// IN the name of Allah
/*
ID: ahmed.m4
PROG:
LANG: C++
*/
//s/stdc++.h>
#include <string.h>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <utility>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <limits.h>
#include <string>
#define PI 3.14159265
#define cell(i,j) ((i)*m+(j))
using namespace std;
int gcd(int a,int b){return b==0?a:gcd(b,a%b);}
int setbit(int n,int idx){return n|1<<idx;}
int countbits(int n){int ret=0;while(n){if(n&1)ret++;n/=2;};return ret;}
int getbit(int n,int idx){return ((n >> idx) & 1) ==1 ;}
typedef pair<int,int>ii;
typedef pair<int,pair<int,int> >iii;
typedef bitset<20>mask;
const long long oo=1000000009;
/*struct node{
bool operator < (node n) const{
return d < n.d;
}
};*/
map<long long,int>mp;
set<long long>s;
long long mx=0,a;
int main()
{
  //freopen("test.in","r",stdin);
  int n;
  cin>>n;
  for(int i=0;i<n;i++)cin>>a,mx=max(a,mx),mp[a]++,s.insert(a);
  set<long long>::iterator it;
  for(it=s.begin();it!=s.end();it++){
    int t=*(it);//cout<<t<<mp[t]<<' ';
    if(mp[t]>2){
        mp[(*it)+1]+=(t/2);
        mx=max(mx,*(it));
        s.insert((*it)+1);
    }
  }
  cout<<mx+1-s.size();
  return 0;
}


// IN the name of Allah
/*
ID: ahmed.m4
PROG:
LANG: C++
*/
//s/stdc++.h>
#include <string.h>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <utility>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <limits.h>
#include <string>
#define PI 3.14159265
#define cell(i,j) ((i)*m+(j))
using namespace std;
int gcd(int a,int b){return b==0?a:gcd(b,a%b);}
int setbit(int n,int idx){return n|1<<idx;}
int countbits(int n){int ret=0;while(n){if(n&1)ret++;n/=2;};return ret;}
int getbit(int n,int idx){return ((n >> idx) & 1) ==1 ;}
typedef pair<int,int>ii;
typedef pair<int,pair<int,int> >iii;
typedef bitset<20>mask;
const long long oo=1000000009;
/*struct node{
bool operator < (node n) const{
return d < n.d;
}
};*/
map<long long,int>mp;
long long mx=0,a,last,c;
int main()
{
  //freopen("test.in","r",stdin);
  int n;
  cin>>n;
  vector<long long>v;
  for(int i=0;i<n;i++){
    cin>>a;
    mp[a]++;
    if(mp[a]==1)v.push_back(a);
  }
  int idx=1;
  queue<long long>q;
  q.push(v[0]);
  while(!q.empty()){
   long long next,m,x=q.front();q.pop();
   c++;
   mx=max(mx,x);
   if(mp[x]>2){
   next=x+1,m=(mp[x]-1)/2;
   mp[x+1]=m;
   if(idx<v.size()&&next<v[idx])
   q.push(next);
   else if(next==v[idx])q.push(next),idx++;
   else if(idx>=v.size())q.push(next);
   }
   else{
    if(idx<v.size())
    q.push(v[idx]);idx++;
   }
  }
  cout<<mx+1-c;
  return 0;
}


#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
using namespace std;
vector<int>v;
map<int,int>mp;
int main()
{
    int n,x;
    cin>>n;
    mp.clear();
    for(int i=1;i<=n;i++)
    {
        cin>>x;
        mp[x]++;
    }
    map<int,int>::iterator it=mp.begin();
    v.clear();
    for(int i=0;i<1000;i++)
        v.push_back(0);
    int mx=0;
    for(;it!=mp.end();it++)
    {
        v[it->first]=(v[it->first]+it->second)%2;
        v[it->first+1]=(v[it->first+1]+it->second)/2;
        mx=max(mx,it->first+1);
    }
    while(v[mx]==0)
        mx--;
    int ans=0;
    for(int i=mx;i>=0;i--)
        if(v[i]==0)
            ans++;
    cout<<ans<<endl;
    //system("pause");
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
using namespace std;
map<int,int>m;
int main()
{
    m.clear();
    int n,x;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>x;
        if(m.find(x)==m.end())
            m[x]=1;
        else
        {
            m.erase(x);
            int tmp=x+1;
            while(m.find(tmp)!=m.end())
            {
                m.erase(tmp);
                tmp++;
            }
            m[tmp]=1;
        }
    }
    map<int,int>::iterator it=m.begin();
    int mx=0;
    for(;it!=m.end();it++)
        mx=max(mx,it->first);
    //cout<<mx<<":"<<m.size()<<endl;
    cout<<1+mx-m.size()<<endl;
    //system("pause");
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
using namespace std;
map<int,int>m;
int main()
{
    m.clear();
    int n,x;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>x;
        if(m.find(x)==m.end())
            m[x]=1;
        else
        {
            m.erase(x);
            int tmp=x+1;
            while(m.find(tmp)!=m.end())
            {
                m.erase(tmp);
                tmp++;
            }
        }
    }
    map<int,int>::iterator it=m.begin();
    int mx=0;
    for(;it!=m.end();it++)
        mx=max(mx,it->first);
    //cout<<mx<<":"<<m.size()<<endl;
    cout<<1+mx-m.size()<<endl;
    //system("pause");
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
using namespace std;
map<int,int>m;
int main()
{
	m.clear();
	int n,x;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>x;
		if(m.find(x)==m.end())
			m[x]++;
		else
		{
			m.erase(x);
			m[x+1]++;
		}
	}
	map<int,int>::iterator it=m.begin();
	int mx=0;
	for(;it!=m.end();it++)
		mx=max(mx,it->first);
	//cout<<mx<<":"<<m.size()<<endl;
	cout<<1+mx-m.size()<<endl;
	//system("pause");
	return 0;
}


#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
using namespace std;
map<int,int>m;
int main()
{
    m.clear();
    int n,x;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>x;
        m[x]++;
    }
    map<int,int>::iterator it=m.begin();
    int ans=0;
    for(;it!=m.end();it++)
    {
        int tmp=it->second/2;
        it->second%=2;
        if(tmp)
        {
                m[it->first+1]++;
        }
    }
    int mx=0;
    for(it=m.begin();it!=m.end();it++)
    {
        //cout<<it->first<<":"<<it->second<<endl;
        mx=max(mx,it->first);
        if(it->second==1)
            ans++;
    }
    cout<<1+mx-ans<<endl;
    //system("pause");
    return 0;

}


#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
using namespace std;
map<int,int>m;
int main()
{
    m.clear();
    int n,x;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>x;
        map<int,int>::iterator it=m.find(x);
        if(it==m.end())
            m[x]=1;
        else
        {
            m.erase(it);
            m[x+1]=1;
        }
    }
    map<int,int>::iterator it=m.begin();
    int mx=0;
    for(;it!=m.end();it++)
        mx=max(mx,it->first);
    //cout<<mx<<":"<<m.size()<<endl;
    cout<<1+mx-m.size()<<endl;
    //system("pause");
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <set>
using namespace std;
set<int>s;
int main()
{
	int n,x;
	s.clear();
	cin>>n;
	int mx=0;
	for(int i=1;i<=n;i++)
	{
		cin>>x;
		if(s.find(x)==s.end())
			s.insert(x);
		else
		{
			while(s.find(x)!=s.end())
			{
				s.erase(x);
				x++;
			}
			s.insert(x);
		}
		mx=max(mx,x);
	}
	cout<<mx+1-s.size()<<endl;
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <cstring>
#include <cmath>
#include <queue>
#include <map>
#include <set>
using namespace std;

#define rep(i,n) for(int i=0; i<n; i++)
#define FOR(i,s,n) for(int i=s; i<n; i++)
#define ALL(x) x.begin(), x.end()
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;

int main() {
	int n;
	cin >> n;
	map<ll, int> mp;
	rep(i,n) {
		ll l;
		cin >> l;
		mp[l]++;
		while(mp[l] == 2) {
			mp[l]--;
			mp[++l]++;
		}
	}
	mp[-1] = 1;
	ll cnt = 0;
	for(map<ll,int>::iterator it=mp.begin(); it!=mp.end(); it++) {
		map<ll,int>::iterator nit = it;
		nit++;
		if(nit != mp.end() && it->second>0 && nit->second>0) {
			cnt += nit->first - it->first - 1;
		}
//		cout << "first=" << it->first << ", second=" << it->second << endl;
	}
	cout << cnt << endl;
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <cstring>
#include <cmath>
#include <queue>
#include <map>
#include <set>
using namespace std;

#define rep(i,n) for(int i=0; i<n; i++)
#define FOR(i,s,n) for(int i=s; i<n; i++)
#define ALL(x) x.begin(), x.end()
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;

int main() {
	int n;
	cin >> n;
	map<ll, int> mp;
	rep(i,n) {
		ll l;
		cin >> l;
		mp[l]++;
		while(mp[l] == 2) {
			mp[l]-=2;
			mp[++l]++;
		}
	}
	mp[-1] = 1;
	ll cnt = 0;
	for(map<ll,int>::iterator it=mp.begin(); it!=mp.end();) {
		while(it != mp.end() && it->second==0) {
			it++;
		}
		if(it==mp.end()) break;

		map<ll,int>::iterator nit = it;
		nit++;
		while(nit != mp.end() && nit->second==0) {
			nit++;
		}
		if(nit==mp.end()) break;

		cnt += nit->first - it->first - 1;
		it = nit;
//		cout << "first=" << it->first << ", second=" << it->second << endl;
	}
	cout << cnt << endl;
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <cstring>
#include <cmath>
#include <queue>
#include <map>
#include <set>
using namespace std;

#define rep(i,n) for(int i=0; i<n; i++)
#define FOR(i,s,n) for(int i=s; i<n; i++)
#define ALL(x) x.begin(), x.end()
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;

int main() {
	int n;
	cin >> n;
	map<ll, int> mp;
	rep(i,n) {
		ll l;
		cin >> l;
		mp[l]++;
		while(mp[l] == 2) {
			mp[l]-=2;
			mp[++l]++;
		}
	}
	mp[-1] = 1;
	ll cnt = 0;
	for(map<ll,int>::iterator it=mp.begin(); it!=mp.end(); it++) {
		map<ll,int>::iterator nit = it;
		nit++;
		if(nit != mp.end() && it->second>0 && nit->second>0) {
			cnt += nit->first - it->first - 1;
		}
//		cout << "first=" << it->first << ", second=" << it->second << endl;
	}
	cout << cnt << endl;
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <cstring>
#include <cmath>
#include <queue>
#include <map>
#include <set>
using namespace std;

#define rep(i,n) for(int i=0; i<n; i++)
#define FOR(i,s,n) for(int i=s; i<n; i++)
#define ALL(x) x.begin(), x.end()
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;

int main() {
	int n;
	cin >> n;
	ll sum = 0;
	rep(i,n) {
		ll l;
		cin >> l;
		sum += pow(2, l);
	}
	int cnt = 0;
	while(sum > 0) {
		if(sum%2 == 0) {
			cnt++;
		}
		sum /= 2;
	}
	cout << cnt << endl;
	return 0;
}


#include<cstdio>
using namespace std;
long long n,tot,p,a[100000009];
int main(){
    scanf("%I64d",&n);
    for(int i=1;i<=n;++i)scanf("%I64d",&p),a[p]++;
        for(int i=0;i<=p;++i){
            if(a[i]){
                long long v=a[i]/2;
                if(v){a[i+1]+=v;if(i+1>p)p=i+1;}
                a[i]-=v*2;
                if(!a[i])tot++;
            }else tot++;
        }
    printf("%I64d\n",tot);
}


#include<cstdio>
using namespace std;
long long n,tot,p,a[10000009];
int main(){
    scanf("%I64d",&n);
    for(int i=1;i<=n;++i)scanf("%I64d",&p),a[p]++;
        for(int i=0;i<=p;++i){
            if(a[i]){
                long long v=a[i]/2;
                if(v){a[i+1]+=v;if(i+1>p)p=i+1;}
                a[i]-=v*2;
                if(!a[i])tot++;
            }else tot++;
        }
    printf("%I64d\n",tot);
}


#include <cstdio>
#include <set>
using namespace std;
set<int>Set;
int main(){
    int n,x,Max=0;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&x);
        while(Set.count(x)) Set.erase(x),x++;
        Max=max(Max,x);
        Set.insert(x);
    }
    printf("%I64\n",Max+1-Set.size());
    return 0;
}


#include <cstdio>
#include <set>
using namespace std;
set<int>Set;
int main(){
    int n,x,Max=0;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&x);
        while(Set.count(x)) Set.erase(x),x++;
        Max=max(Max,x);
        Set.insert(x);
    }
    printf("%I64d\n",(long long)(Max+1-Set.size()));
    return 0;
}


#include <stdio.h>
#include <cstring>
#include <vector>
#include <map>
#include <algorithm>

using namespace std;

#define pb push_back

int main(){
  int n;
  scanf("%d", &n);
  int ans = 0, last_pos = -1;
  int last_val = -1;
  for( int i = 0; i < n; i++){
    int va;
    scanf("%d", &va);
    ans += max(va - last_pos - 1,0);
    if( va == last_val ) last_pos++;
    else last_pos = max(last_pos + 1,va);
    last_val = va;
  }
  printf("%d\n",ans);
  return 0;
}


#include <stdio.h>
#include <cstring>
#include <vector>
#include <map>
#include <algorithm>

using namespace std;

#define pb push_back

int main(){
  int n;
  scanf("%d", &n);
  int ans = 0, last_pos = -1;
  for( int i = 0; i < n; i++){
    int va;
    scanf("%d", &va);
    int now_pos = max(last_pos + 1,va);
    ans += max(now_pos - last_pos - 1,0);
    last_pos = now_pos;
  }
  printf("%d\n",last_pos - n + 1);
  return 0;
}


#include <stdio.h>
#include <cstring>
#include <vector>
#include <map>
#include <algorithm>

using namespace std;

#define pb push_back

int main(){
  int n;
  scanf("%d", &n);
  int ans = 0, last_pos = -1;
  int last_val = -1;
  for( int i = 0; i < n; i++){
    int va;
    scanf("%d", &va);
    int now_pos = max(last_pos + 1,va);
    ans += max(now_pos - last_pos - 1,0);
    last_pos = now_pos;
    last_val = va;
  }
  printf("%d\n",ans);
  return 0;
}


#include <cstring>
#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <algorithm>

using namespace std;

typedef long long lint;

int main(){
  int n;
  scanf("%d", &n);
  set < lint > pos;
  for( int i = 0; i < n; i++){
    lint va;
    scanf("%lld", &va);
    while( pos.find(va) != pos.end() ){
      pos.erase(va);
      va++;
    }
    pos.insert(va);
  }
  set<lint>::iterator it = pos.end();
  if( pos.size() > 0 ) it--;
  printf("%lld\n", (*it) - pos.size() + 1);
  return 0;
}


#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <ctime>
#include <algorithm>
#include <sstream>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <iterator>
#include <functional>
#include <bitset>

#define mp make_pair
#define pb push_back

using namespace std;

typedef long long ll;
typedef long double ld;

int main()
{
    long long n;
    cin >> n;
    ll A[n];
    for (long long i = 0; i < n; i++)
        cin >> A[i];
    if (n == 1)
    {
        cout << A[0] << endl;
        return 0;
    }
    vector <long long> C;
    long long l = 0;
    for (long long i = 0; i < n - 1; i++)
    {
        if (A[i] != A[i + 1])
        {
            C.push_back(i - l + 1);
            l = i + 1;
        }
    }
    long long ANS = 0;
    /*
    C.push_back(n - 1 - l + 1);
    for (long long i = 0; i < C.size(); i++)
        cout << C[i] << " ";
    cout << endl;
    */
    long long k = 0;
    for (long long i = 1; i < n; i++)
    {
        if (A[i] != A[i - 1])
        {
            long long su = 0;
            long long z = 2;
            int b = 0;
            long long t = A[i] - A[i - 1] - 1;
            while (1)
            {
                if (su == t)
                {
                    b = 1;
                    break;
                }
                if (C[k] - z >= 1)
                {
                    C[k] -= z;
                    z *= 2;
                    su++;
                }
                else
                    break;
            }
            if (su < (A[i] - A[i - 1] - 1))
                ANS += (A[i] - A[i - 1] - 1 - su);
            k++;
            z = 1;
            int bb = 0;
            for (long long st = 1; st <= A[i] - A[i - 1]; st++)
            {
                z *= 2;
                if (z > C[k - 1])
                    break;
                if (st == A[i] - A[i - 1])
                    bb = 1;
            }
            if (bb == 1)
            {
                C[k] += C[k - 1] / z;
            }
        }
    }
    ANS += A[0];
    cout << ANS << endl;
    return 0;
}


#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <ctime>
#include <algorithm>
#include <sstream>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <iterator>
#include <functional>
#include <bitset>

#define mp make_pair
#define pb push_back

using namespace std;

typedef long long ll;
typedef long double ld;

long long gcd(long long a, long long b)
{
    if (b == 0)
        return a;
    else
        return (gcd(b, a % b));
}

int main()
{
    int n;
    cin >> n;
    vector <long long> V;
    V.resize(n);
    map <long long, long long> M;
    map <long long, long long> :: iterator it;
    for (int i = 0; i < n; i++)
    {
        cin >> V[i];
        if (M.count(V[i]) == 0)
            M[V[i]] = 1;
        else
            M[V[i]]++;
    }
    long long k = -1;
    while (k == -1 || k == 1)
    {
        k = 0;
        for (it = M.begin(); it != M.end(); it++)
        {
            if ((*it).second > 1)
            {
                long long Z = (*it).first;

                if (M.count(Z + 1) == 0)
                    M[Z + 1] = (*it).second / 2;
                else
                    M[Z + 1] += (*it).second / 2;
                M[Z] = M[Z] % 2;
                k = 1;
            }
        }
    }

    set <long long> S;
    vector <long long> K;
    long long ma = -1;
    for (it = M.begin(); it != M.end(); it++)
    {
        if ((*it).second)
        {
            //cout << (*it).first << endl;

            ma = max(ma, (*it).first);
            S.insert((*it).first);
        }
    }
    cout << ma - S.size() + 1;
    return 0;
}


#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <ctime>
#include <algorithm>
#include <sstream>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <iterator>
#include <functional>
#include <bitset>

#define mp make_pair
#define pb push_back

using namespace std;

typedef long long ll;
typedef long double ld;

int main()
{
    long long n;
    cin >> n;
    ll A[n];
    for (long long i = 0; i < n; i++)
        cin >> A[i];
    if (n == 1)
    {
        cout << A[0] << endl;
        return 0;
    }
    vector <long long> C;
    long long l = 0;
    for (long long i = 0; i < n - 1; i++)
    {
        if (A[i] != A[i + 1])
        {
            C.push_back(i - l + 1);
            l = i + 1;
        }
    }
    long long ANS = 0;
    /*
    C.push_back(n - 1 - l + 1);
    for (long long i = 0; i < C.size(); i++)
        cout << C[i] << " ";
    cout << endl;
    */
    long long k = 0;
    for (long long i = 1; i < n; i++)
    {
        if (A[i] != A[i - 1])
        {
            long long su = 0;
            long long z = 2;
            while (1)
            {
                if (C[k] - z >= 1)
                {
                    C[k] -= z;
                    z *= 2;
                    su++;
                }
                else
                    break;
            }
            if (su < (A[i] - A[i - 1] - 1))
                ANS += (A[i] - A[i - 1] - 1 - su);
            k++;
        }
    }
    ANS += A[0];
    cout << ANS << endl;
    return 0;
}


#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <ctime>
#include <algorithm>
#include <sstream>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <iterator>
#include <functional>
#include <bitset>

#define mp make_pair
#define pb push_back

using namespace std;

typedef long long ll;
typedef long double ld;

long long gcd(long long a, long long b)
{
    if (b == 0)
        return a;
    else
        return (gcd(b, a % b));
}

int main()
{
    /*
    int n;
    string S[n];
    for (int i = 0; i < n; i++)
        cin >> S[i];
    int ma = -1;
    for (int i = 0; i < n; i++)
        ma = max(ma, (int)S[i].size());
    for (int i = 0; i < n; i++)
        while (S[i].size() < ma)
            S[i] = '0' + S[i];
    */
    int n;
    cin >> n;
    long long A[n];
    set <long long> S;
    for (int i = 0; i < n; i++)
    {
        cin >> A[i];
        S.insert(A[i]);
    }
    cout << A[n - 1] - (long long)(S.size()) + 1;
    return 0;
}


#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <ctime>
#include <algorithm>
#include <sstream>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <iterator>
#include <functional>
#include <bitset>

#define mp make_pair
#define pb push_back

using namespace std;

typedef long long ll;
typedef long double ld;

int main()
{
    int n;
    cin >> n;
    ll A[n];
    for (int i = 0; i < n; i++)
        cin >> A[i];
    if (n == 1)
    {
        cout << A[0] << endl;
        return 0;
    }
    vector <long long> C;
    int l = 0;
    for (int i = 0; i < n - 1; i++)
    {
        if (A[i] != A[i + 1])
        {
            C.push_back(i - l + 1);
            l = i + 1;
        }
    }
    long long ANS = 0;
    C.push_back(n - 1 - l + 1);
    /*
    for (int i = 0; i < C.size(); i++)
        cout << C[i] << " ";
    cout << endl;
    */
    int k = 0;
    for (int i = 1; i < n; i++)
    {
        if (A[i] != A[i - 1])
        {
            long long su = 0;
            long long z = 2;
            while (1)
            {
                if (C[k] - z >= 1)
                {
                    C[k] -= z;
                    z *= 2;
                    su++;
                }
                else
                    break;
            }
            if (su < (A[i] - A[i - 1] - 1))
                ANS += (A[i] - A[i - 1] - 1 - su);
            k++;
        }
    }
    cout << ANS << endl;
    return 0;
}


#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <ctime>
#include <algorithm>
#include <sstream>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <iterator>
#include <functional>
#include <bitset>

#define mp make_pair
#define pb push_back

using namespace std;

typedef long long ll;
typedef long double ld;

int main()
{
    int n;
    cin >> n;
    ll A[n];
    for (int i = 0; i < n; i++)
        cin >> A[i];
    if (n == 1)
    {
        cout << A[0] << endl;
        return 0;
    }
    vector <long long> C;
    int l = 0;
    for (int i = 0; i < n - 1; i++)
    {
        if (A[i] != A[i + 1])
        {
            C.push_back(i - l + 1);
            l = i + 1;
        }
    }
    long long ANS = 0;
    /*
    C.push_back(n - 1 - l + 1);
    for (int i = 0; i < C.size(); i++)
        cout << C[i] << " ";
    cout << endl;
    */
    int k = 0;
    for (int i = 1; i < n; i++)
    {
        if (A[i] != A[i - 1])
        {
            long long su = 0;
            long long z = 2;
            while (1)
            {
                if (C[k] - z >= 1)
                {
                    C[k] -= z;
                    z *= 2;
                    su++;
                }
                else
                    break;
            }
            if (su < (A[i] - A[i - 1] - 1))
                ANS += (A[i] - A[i - 1] - 1 - su);
            k++;
        }
    }
    ANS += A[0];
    cout << ANS << endl;
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
using namespace std;

#define EPS 1e-8
#define MAXN 100005
#define PI acos(-1.0)

int n;
map<int, int> m;
map<int, int>::iterator mit;
vector<int> v;

void dfs(int f, int s)
{
	if (s == 1) return;
	int c = 1, flag = 0;
	if (s & 1) {
		flag = 1;
	}
	s >>= 1;
	while (s > 0) {
		if (s & 1) {
			m[f + c] += 1;
			dfs(f + c, m[f + c]);
		}
		s >>= 1;
		c++;
	}
	//printf("ri\n");
	if (flag) m[f] = 1;
	else m.erase(f);
}

int main()
{
	scanf("%d", &n);
	for (int i = 0, a; i < n; ++i) {
		scanf("%d", &a);
		m[a]++;
	}
		for (mit = m.begin(); mit != m.end(); ++mit) {
			dfs(mit->first, mit->second);
			/*
			int d = mit->second;
			if (d & 1) m[mit->first] = 1;
			d -= 1;
			int c = 0;
			while (d > 0) {
				if (d & 1) m[mit->first + c] += 1;
				d >>= 1;
				c <<= 1;
			}
			*/
		}
	for (mit = m.begin(); mit != m.end(); ++mit) {
		v.push_back(mit->first);
	}
	sort(v.begin(), v.end());
	//printf("max = %d\n", v[v.size() - 1]);
	printf("%d\n", v[v.size() - 1] - (int)v.size() + 1);
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
using namespace std;

#define EPS 1e-8
#define MAXN 100005
#define PI acos(-1.0)

int n;
map<int, int> m;
map<int, int>::iterator mit;
vector<int> v;

void dfs(int f, int s)
{
	if (s == 1) return;
	int c = 1, flag = 0;
	if (s & 1) {
		flag = 1;
		s >>= 1;
	}
	while (s > 0) {
		if (s & 1) {
			m[f + c] += 1;
			dfs(f + c, m[f + c]);
		}
		s >>= 1;
		c++;
	}
	//printf("55\n");
	if (flag) m[f] = 1;
}

int main()
{
	scanf("%d", &n);
	for (int i = 0, a; i < n; ++i) {
		scanf("%d", &a);
		m[a]++;
	}
		for (mit = m.begin(); mit != m.end(); ++mit) {
			dfs(mit->first, mit->second);
			/*
			int d = mit->second;
			if (d & 1) m[mit->first] = 1;
			d -= 1;
			int c = 0;
			while (d > 0) {
				if (d & 1) m[mit->first + c] += 1;
				d >>= 1;
				c <<= 1;
			}
			*/
		}
	for (mit = m.begin(); mit != m.end(); ++mit) {
		v.push_back(mit->first);
	}
	sort(v.begin(), v.end());
	//printf("max = %d\n", v[v.size() - 1]);
	printf("%d\n", v[v.size() - 1] - (int)v.size() + 1);
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
using namespace std;

#define EPS 1e-8
#define MAXN 100005
#define PI acos(-1.0)

int n;
map<int, int> m;
map<int, int>::iterator mit;
vector<int> v;

void dfs(int f, int s)
{
	if (s == 1) return;
	int c = 1, flag = 0;
	if (s & 1) {
		flag = 1;
		s >>= 1;
	}
	while (s > 0) {
		if (s & 1) {
			m[f + c] += 1;
			dfs(f + c, m[f + c]);
		}
		s >>= 1;
		c <<= 1;
	}
	//printf("55\n");
	if (flag) m[f] = 1;
}

int main()
{
	scanf("%d", &n);
	for (int i = 0, a; i < n; ++i) {
		scanf("%d", &a);
		m[a]++;
	}
		for (mit = m.begin(); mit != m.end(); ++mit) {
			dfs(mit->first, mit->second);
			/*
			int d = mit->second;
			if (d & 1) m[mit->first] = 1;
			d -= 1;
			int c = 0;
			while (d > 0) {
				if (d & 1) m[mit->first + c] += 1;
				d >>= 1;
				c <<= 1;
			}
			*/
		}
	for (mit = m.begin(); mit != m.end(); ++mit) {
		v.push_back(mit->first);
	}
	sort(v.begin(), v.end());
	//printf("max = %d\n", v[v.size() - 1]);
	printf("%d\n", v[v.size() - 1] - (int)v.size() + 1);
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
using namespace std;

#define EPS 1e-8
#define MAXN 100005
#define PI acos(-1.0)

int n;
map<int, int> m;
map<int, int>::iterator mit;
vector<int> v;

void dfs(int f, int s)
{
	if (s == 1) return;
	int c = 1, flag = 0;
	if (s & 1) {
		flag = 1;
	}
	s >>= 1;
	while (s > 0) {
		if (s & 1) {
			m[f + c] += 1;
			dfs(f + c, m[f + c]);
		}
		s >>= 1;
		c++;
	}
	//printf("ri\n");
	if (flag) m[f] = 1;
	else m[f] = 0;
}

int main()
{
	scanf("%d", &n);
	for (int i = 0, a; i < n; ++i) {
		scanf("%d", &a);
		m[a]++;
	}
		for (mit = m.begin(); mit != m.end(); ++mit) {
			dfs(mit->first, mit->second);
			/*
			int d = mit->second;
			if (d & 1) m[mit->first] = 1;
			d -= 1;
			int c = 0;
			while (d > 0) {
				if (d & 1) m[mit->first + c] += 1;
				d >>= 1;
				c <<= 1;
			}
			*/
		}
	for (mit = m.begin(); mit != m.end(); ++mit) {
		if (mit->second != 0)
		v.push_back(mit->first);
	}
	sort(v.begin(), v.end());
	//printf("max = %d\n", v[v.size() - 1]);
	printf("%d\n", v[v.size() - 1] - (int)v.size() + 1);
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
using namespace std;

#define EPS 1e-8
#define MAXN 100005
#define PI acos(-1.0)

int n;
map<int, int> m;
map<int, int>::iterator mit;
vector<int> v;

int main()
{
	scanf("%d", &n);
	for (int i = 0, a; i < n; ++i) {
		scanf("%d", &a);
		m[a]++;
	}
	int t = 100;
	while (t--) {
		for (mit = m.begin(); mit != m.end(); ++mit) {
			int d = mit->second;
			if (d & 1) m[mit->first] = 1;
			d -= 1;
			int c = 0;
			while (d > 0) {
				if (d & 1) m[mit->first + c] += 1;
				d >>= 1;
				c <<= 1;
			}
		}
	}
	for (mit = m.begin(); mit != m.end(); ++mit) {
		v.push_back(mit->first);
	}
	sort(v.begin(), v.end());
	//printf("max = %d\n", v[v.size() - 1]);
	printf("%d\n", v[v.size() - 1] - (int)v.size() + 1);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
#include <queue>
using namespace std;

const int Maxn = 1e5 + 9;
#define ll long long

int main()
{
//    freopen("in.txt", "r", stdin);
    int n;
    cin >> n;
    set<ll> s, ans;
    for(int i = 1; i <= n; ++ i)
    {
        ll a; cin >> a;
        if(!ans.count(a))
        {
            ans.insert(a);
        }
        else if(s.count(a))
        {
            if(!ans.count(a + 1))
            {
                ans.insert(a + 1);
                s.erase(a);
            }
            else
            {
                s.insert(a + 1);
            }
        }
        else s.insert(a);
    }
    cout << (*--ans.end() - ans.size() + 1) << endl;

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;
#define ll long long
struct Data
{
    ll v, num;
    Data() {}
    Data(ll _v, ll _num): v(_v), num(_num) {}
};
const int Maxn = 1e5 + 9;
ll a[Maxn];
const ll inf = 0x7ffffffffffffffLL;
int main()
{
//    freopen("in.txt", "r",stdin);
    vector<Data> vec;
    int n;
    cin >> n;

    for(int i = 1; i <= n; ++ i) cin >> a[i];
    int num = 1;
    for(int i = 2; i <= n; ++ i)
    {
        if(a[i] == a[i - 1]) num ++;
        else
        {
            vec.push_back(Data(a[i - 1], num));
            num = 1;
        }
    }
    vec.push_back(Data(a[n], num));
    vec.push_back(Data(inf, 0));
    ll cnt, cntN = 0;
    ll c = 0, ok = 0;
    for(int i = 0; i < vec.size();)
    {
        if(cntN == 0)
        {
            cnt = vec[i].v;
            cntN = vec[i].num;
            i ++;
        }
        else
        {
            if(cnt < vec[i].v)
            {
                cnt ++;
                cntN --;
                if(cntN == 0) ok ++;
                if(cntN & 1) c ++;
                cntN /= 2;
            }
            else if(cnt == vec[i].v)
            {
                cntN += vec[i].v;
                i ++;
            }
            else i ++;
        }
    }

    cout << (c + (vec.end() - 2)->v + 1 - ok ) << endl;

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;
#define ll long long
struct Data
{
    ll v, num;
    Data() {}
    Data(ll _v, ll _num): v(_v), num(_num) {}
};
const int Maxn = 1e5 + 9;
ll a[Maxn];
const ll inf = 0x7ffffffffffffffLL;
int main()
{
//    freopen("in.txt", "r",stdin);

    vector<Data> vec;
    int n;
    cin >> n;

    for(int i = 1; i <= n; ++ i) cin >> a[i];
    int num = 1;
    for(int i = 2; i <= n; ++ i)
    {
        if(a[i] == a[i - 1]) num ++;
        else
        {
            vec.push_back(Data(a[i - 1], num));
            num = 1;
        }
    }
    vec.push_back(Data(a[n], num));
    vec.push_back(Data(inf, 0));
    ll cnt = vec[0].v, cntN = 0;
    ll c = 0, ok = 0;
    for(int i = 0; i < vec.size();)
    {
        if(cntN == 0)
        {
            cnt = vec[i].v;
            cntN = vec[i].num;
            i ++;
            continue;
        }
        if(cnt < vec[i].v)
        {
            ok ++;
            if(cntN == 1)
            {
                if(i == vec.size() - 1)
                {
                    break;
                }
                else
                {
                    cntN = 0;
                }
            }
            else
            {
                cnt ++;
                cntN --;
                if(cntN & 1) c ++, cntN ++;
                cntN /= 2;

            }

        }
        else if(cnt == vec[i].v)
        {
            cntN += vec[i].v;
            i ++;
        }
        else i ++;
    }
//    cout << "KKK" << ok << " " << c << " " << cnt << endl;
    cout << (c + cnt + 1 - ok ) << endl;

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
#include <queue>
using namespace std;

const int Maxn = 1e5 + 9;
#define ll long long

int main()
{
//    freopen("in.txt", "r", stdin);
    int n;
    cin >> n;
    set<ll> s, ans;
    for(int i = 1; i <= n; ++ i)
    {
        ll a; cin >> a;
        for(;;)
        {
            if(!ans.count(a))
            {
                ans.insert(a); break;
            }
            else
            {
                if(!s.count(a))
                {
                    s.insert(a); break;
                }
                else
                {
                    s.erase(a);
                    a ++;
                }
            }
        }

    }
    cout << (*--ans.end() - ans.size() + 1) << endl;

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;
#define ll long long
struct Data
{
    ll v, num;
    Data() {}
    Data(ll _v, ll _num): v(_v), num(_num) {}
};
const int Maxn = 1e5 + 9;
ll a[Maxn];
const ll inf = 0x7ffffffffffffffLL;
int main()
{
//    freopen("in.txt", "r",stdin);

    vector<Data> vec;
    int n;
    cin >> n;

    for(int i = 1; i <= n; ++ i) cin >> a[i];
    int num = 1;
    for(int i = 2; i <= n; ++ i)
    {
        if(a[i] == a[i - 1]) num ++;
        else
        {
            vec.push_back(Data(a[i - 1], num));
            num = 1;
        }
    }
    vec.push_back(Data(a[n], num));
    vec.push_back(Data(inf, 0));
    ll cnt, cntN = 0;
    ll c = 0, ok = 0;
    for(int i = 0; i < vec.size();)
    {
        if(cntN == 0)
        {
            cnt = vec[i].v;
            cntN = vec[i].num;
            i ++;
        }
        else
        {
            if(cnt < vec[i].v)
            {
                cnt ++;
                cntN --;
                if(cntN == 0) ok ++;
                if(cntN & 1) c ++, cntN ++;
                cntN /= 2;
            }
            else if(cnt == vec[i].v)
            {
                cntN += vec[i].v;
                i ++;
            }
            else i ++;
        }
    }

    cout << (c + (vec.end() - 2)->v + 1 - ok ) << endl;

    return 0;
}


#include <iostream>

using namespace std;

const long long maxn = 1000 * 100 + 10;
long long n;
bool a[maxn];

int main()
{
	cin >> n;
	int temp;
	int maximum = 0;
	int co = 0;
	for(long long i = 0; i < n; i++)
	{
		cin >> temp;
		a[temp] = true;
		if(temp > maximum)
			maximum = temp;
	}
	for(int i = 0; i < maximum; i++)
		if(!a[i])
			co++;
	cout << co << endl;
}


#include <iostream>

using namespace std;

const long long maxn = 1000 * 100;
long long n, a[maxn], sum = 0;

int main()
{
	cin >> n;
	sum += n;
	for(long long i = 0; i < n; i++)
	{
		cin >> a[i];
		sum += a[i];
	}
	long long num = 1;
	for(; num < sum; num *= 2);
	num--;
	num -= sum;
	string s;
	while(num)
	{
		s += (num % 2 + '0');
		num /= 10;
	}
	int l = 0;
	for(int i = 0; i < s.length(); i++)
		if(a[i] == '1')
			l++;
	cout << l << endl;

}


#include <iostream>
#include <set>
using namespace std;

const long long maxn = 1000 * 100;
long long n;
set <long long> myset;

int main()
{
	cin >> n;
	int temp;
	int maximum = 0;
	int co = 0;
	for(long long i = 0; i < n; i++)
	{
		cin >> temp;
		if(temp > maximum)
			maximum = temp;
		if(myset.find(temp) != myset.end())
		{
			int i = temp;
			while(myset.find(i) != myset.end())
			{
				myset.erase(i);
				i++;
			}
			myset.insert(i);
			if(i > maximum)
				maximum = i;
		}
		else
			myset.insert(temp);
	}
	cout << maximum - myset.size() + 1 << endl;
}


#include <iostream>

using namespace std;

const long long maxn = 1000 * 100;
long long n, a[maxn], sum = 0, two = 1;;
long long num = 1;

int main()
{
	cin >> n;
	for(long long i = 0, k = 0; i < n; i++)
	{
		cin >> a[i];
		while(k < a[i])
		{
			k++;
			two *= 2;
		}
		sum += two;
		while(num <= sum)
			num *= 2;
	}
	num--;
	num -= sum;
	string s;
	while(num)
	{
		s += (num % 2 + '0');
		num /= 2;
	}
	int l = 0;
	for(int i = 0; i < s.length(); i++)
		if(s[i] == '1')
			l++;
	cout << l << endl;

}


#include <iostream>

using namespace std;

const long long maxn = 1000 * 100;
long long n, a[maxn], sum = 0, two = 1;;

int main()
{
	cin >> n;
	for(long long i = 0, k = 0; i < n; i++)
	{
		cin >> a[i];
		while(k < a[i])
		{
			k++;
			two *= 2;
		}
		sum += two;
	}
	long long num = 1;
	for(; num <= sum; num *= 2);
	num--;
	num -= sum;
	string s;
	while(num)
	{
		s += (num % 2 + '0');
		num /= 2;
	}
	int l = 0;
	for(int i = 0; i < s.length(); i++)
		if(s[i] == '1')
			l++;
	cout << l << endl;

}


#include <iostream>

using namespace std;

const long long maxn = 1000 * 100;
long long n, a[maxn], sum = 0, two = 1;;
long long num = 1;

int main()
{
	cin >> n;
	for(long long i = 0, k = 0; i < n; i++)
	{
		cin >> a[i];
		while(k < a[i])
		{
			k++;
			two *= 2;
		}
		sum += two;
	}
	string s;
	while(sum)
	{
		s += (sum % 2 + '0');
		sum /= 2;
	}
	int l = 0;
	for(int i = 0; i < s.length(); i++)
		if(s[i] == '0')
			l++;
	cout << l << endl;

}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>

using namespace std;

int a[3], n, m=0, ans=0, k;

int main()
{
    memset(a,sizeof(a),0);
    scanf("%d",&n);
    a[0] = 0; a[1] = 0;
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&k);
        if(a[0] == k) a[1]++;
        else
        {
            while( a[1] > 0 && a[0] < k)
            {
                a[0] = a[0]+1;
                if(a[1] % 2 == 0) ans++;
                a[1] /= 2;
            }
            a[0] = k;
            if( a[0] < k ) { ans += k - a[0]; a[1] = 1; }
            else a[1] ++ ;
        }
    }
    while( a[1] > 0)
    {
        a[0] = a[0]+1;
        if(a[1] % 2 == 0) ans++;
        a[1] /= 2;
    }
    printf("%d\n",ans);

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>

using namespace std;

int a[50], n, m=0, ans=0, k;

int main()
{
    memset(a,sizeof(a),0);
    scanf("%d",&n);
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&k);
        a[k]++;
        m = max(m,k);
    }
    for(int i=0; i<=m; i++)
    {
        if( a[i] > 1) { a[i+1] += a[i] / 2; a[i] = 1; if(i+1>m) m = i+1; }
        else ans += 1 - a[i];
    }
    printf("%d\n",ans);

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>

using namespace std;

int a[550], n, m=0, ans=0, k;

int main()
{
    memset(a,sizeof(a),0);
    scanf("%d",&n);
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&k);
        a[k]++;
        m = max(m,k);
    }
    for(int i=0; i<=m; i++)
    {
        if( a[i] > 1) { a[i+1] += a[i] / 2; a[i] = 1; if(i+1>m) m = i+1; }
        else ans += 1 - a[i];
    }
    printf("%d\n",ans);

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>

using namespace std;

int a[3], n, m=0, ans=0, k;

int main()
{
    memset(a,sizeof(a),0);
    scanf("%d",&n);
    a[0] = -1; a[1] = 0;
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&k);
        if(a[0] == k) a[1]++;
        else if(a[0]+1 == k) { a[0] = k; a[1] = a[1] / 2; }
        else
        {
            while( a[1] > 0 && a[0] < k)
            {
                a[0] = a[0]+1;
                a[1] /= 2;
            }
            if( a[0] < k ) { ans += k - a[0]-1; a[1] = k; a[0] = 1; }
        }
    }
    printf("%d\n",ans);

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>

using namespace std;

int a[3], n, m=0, ans=0, k;

int main()
{
    memset(a,sizeof(a),0);
    scanf("%d",&n);
    a[0] = 0; a[1] = 0;
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&k);
        if(a[0] == k) a[1]++;
        else
        {
            while( a[1] > 0 && a[0] < k)
            {
                a[0] = a[0]+1;
                if(a[1] % 2 == 0) ans++;
                a[1] /= 2;
            }
            if( a[0] < k ) { ans += k - a[0]; }
            a[0] = k; a[1] = 1;
        }
    }
    while( a[1] > 0)
    {
        a[0] = a[0]+1;
        if(a[1] % 2 == 0) ans++;
        a[1] /= 2;
    }
    printf("%d\n",ans);

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>

using namespace std;

int a[3], n, m=0, ans=0, k;

int main()
{
    memset(a,sizeof(a),0);
    scanf("%d",&n);
    a[0] = 0; a[1] = 0;
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&k);
        if(a[0] == k) a[1]++;
        else
        {
            while( a[1] > 0 && a[0] < k)
            {
                a[0] = a[0]+1;
                if(a[1] % 2 == 0) ans++;
                a[1] /= 2;
            }
            if( a[0] < k ) { ans += k - a[0]; a[1] = 1; }
            else a[1] ++ ;
            a[0] = k;
        }
    }
    while( a[1] > 0)
    {
        a[0] = a[0]+1;
        if(a[1] % 2 == 0) ans++;
        a[1] /= 2;
    }
    printf("%d\n",ans);

    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>

using namespace std;

#define print(x) cout<<x<<endl
#define input(x) cin>>x

const int SIZE = 102400;

int n;

int main()
{
    int a;
    while(input(n))
    {
        map<int, int> top;
        for(int i=0;i<n;i++)
        {
            input(a);
            top[a]++;
        }
        int ans = 0, pre = 0;
        for(map<int, int>::iterator iter=top.begin();
             iter!=top.end(); iter++)
        {
            int i = iter->first;
            if(top[i]/2)
            {
                top[i+1] += top[i]/2;
                top[i] = top[i]%2;
            }

            if(top[i])
            {
                ans += i - pre;
                pre = i;
            }
        }
        print(ans);
    }
    return 0;
}


#include <bits/stdc++.h>
#define INT long long int
#define MOD 1000000007
using namespace std;
INT n, m, mn = 987654321, ans = 987654321, val, Tree[1<<20], base, hole;
set<INT> S, V;
unordered_map<INT, INT> mp, ump;
vector<pair<INT, INT> > H;
void update(INT x)
{
    while( x )
        Tree[x] = Tree[2*x] + Tree[2*x+1], x>>=1;
}
INT sum(INT s, INT e)
{
    INT ret = 0;
    while( s <= e )
    {
        if( s%2 == 1 ) ret += Tree[s], s++;
        if( e%2 == 0 ) ret += Tree[e], e--;
        s>>=1, e>>=1;
    }
    return ret;
}
int main(){
    ios::sync_with_stdio(false);
    cin>>n>>m;
    for(int i = 0 ; i < n ; i++ )
    {
        INT x;
        cin>>x;
        V.insert(x);
        S.insert(x);
    }
    V.insert(1000000000);
    S.insert(1000000000);
V.insert(1);
    S.insert(1);
    for(int i = 0 ; i < m ; i++ )
    {
        INT x, y, z;
        cin>>x>>y>>z;
        H.push_back(make_pair(x, y));
        S.insert(x);
        S.insert(y);
        mn = min(x, mn);
    }
    if( mn > 1 ) return cout<<0, 0;
    sort(H.begin(), H.end());
    for(INT x : S ) mp[x] = val, ump[val] = x, val++;
    for( base = 1 ; base < val ; base<<=1 );
    for(int i = 0, j = 0, pre = 0, vc = 0 ; i < val ; i++ )
    {
        while( j < m and mp[H[j].first] <= i  )
        {
            Tree[mp[H[j].second] + base] += 1;
            update((mp[H[j].second]+base)>>1);
            j++;
        }
        if( V.find(ump[i]) != V.end() )
        {
            ans = min( ans, Tree[1] + vc );
            pre = i;
            vc++;
        }
        Tree[i+base] = 0, update((i+base)>>1);
    }
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
#define INT long long int
#define MOD 1000000007
using namespace std;
INT n, m, mn = 987654321, ans = 987654321, val, Tree[1<<20], base, vc;
set<INT> S, V;
unordered_map<INT, INT> mp, ump;
vector<pair<INT, INT> > H;
void update(INT x)
{
    while( x )
        Tree[x] = Tree[2*x] + Tree[2*x+1], x>>=1;
}
INT sum(INT s, INT e)
{
    INT ret = 0;
    while( s <= e )
    {
        if( s%2 == 1 ) ret += Tree[s], s++;
        if( e%2 == 0 ) ret += Tree[e], e--;
        s>>=1, e>>=1;
    }
    return ret;
}
int main(){
    ios::sync_with_stdio(false);
    cin>>n>>m;
    for(int i = 0 ; i < n ; i++ )
    {
        INT x;
        cin>>x;
        V.insert(x);
        S.insert(x);
    }
    V.insert(1000000000);
    S.insert(1000000000);
    if( V.find(1) == S.end() )
    {
        V.insert(1);
        S.insert(1);
        vc--;
    }
    for(int i = 0 ; i < m ; i++ )
    {
        INT x, y, z;
        cin>>x>>y>>z;
        H.push_back(make_pair(x, y));
        S.insert(x);
        S.insert(y);
        mn = min(x, mn);
    }
    if( mn > 1 ) return cout<<0, 0;
    sort(H.begin(), H.end());
    for(INT x : S ) mp[x] = val, ump[val] = x, val++;
    for( base = 1 ; base < val ; base<<=1 );
    for(int i = 0, j = 0, pre = 0 ; i < val ; i++ )
    {
        while( j < m and mp[H[j].first] <= i  )
        {
            Tree[mp[H[j].second] + base] += 1;
            update((mp[H[j].second]+base)>>1);
            j++;
        }
        if( V.find(ump[i]) != V.end() )
        {
            ans = min( ans, Tree[1] + vc );
            pre = i;
            vc++;
        }
        Tree[i+base] = 0, update((i+base)>>1);
    }
    cout<<(ans<0?0:ans);
    return 0;
}


#include <bits/stdc++.h>
#define INT long long int
#define MOD 1000000007
using namespace std;
INT n, m, ans = 987654321, val, Tree[1<<20], base, hole;
set<INT> S, V;
unordered_map<INT, INT> mp, ump;
vector<pair<INT, INT> > H;
void update(INT x)
{
    while( x )
        Tree[x] = Tree[2*x] + Tree[2*x+1], x>>=1;
}
INT sum(INT s, INT e)
{
    INT ret = 0;
    while( s <= e )
    {
        if( s%2 == 1 ) ret += Tree[s], s++;
        if( e%2 == 0 ) ret += Tree[e], e--;
        s>>=1, e>>=1;
    }
    return ret;
}
int main(){
    ios::sync_with_stdio(false);
    cin>>n>>m;
    for(int i = 0 ; i < n ; i++ )
    {
        INT x;
        cin>>x;
        V.insert(x);
        S.insert(x);
    }
    for(int i = 0 ; i < m ; i++ )
    {
        INT x, y, z;
        cin>>x>>y>>z;
        if( z == 1000000000 ) continue;
        H.push_back(make_pair(x, y));
        S.insert(x);
        S.insert(y);
        if( x == 1 and y == 1000000000 ) hole++;
    }
    if( n == 0 ) return cout<<hole, 0;
    sort(H.begin(), H.end());
    for(INT x : S ) mp[x] = val, ump[val] = x, val++;
    for( base = 1 ; base < val ; base<<=1 );
    for(int i = 0, j = 0, pre = 0, vc = 0 ; i < val ; i++ )
    {
        while( j < m and mp[H[j].first] <= i  )
        {
            Tree[mp[H[j].second] + base] += 1;
            update((mp[H[j].second]+base)>>1);
            j++;
        }
        if( V.find(ump[i]) != V.end() )
        {
            ans = min( ans, Tree[1] + vc );
            pre = i;
            vc++;
        }
        Tree[i+base] = 0, update((i+base)>>1);
    }
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
#define INT long long int
#define MOD 1000000007
using namespace std;
INT n, m, ans = 987654321, val, Tree[1<<20], base, hole;
set<INT> S, V;
unordered_map<INT, INT> mp, ump;
vector<pair<INT, INT> > H;
void update(INT x)
{
    while( x )
        Tree[x] = Tree[2*x] + Tree[2*x+1], x>>=1;
}
INT sum(INT s, INT e)
{
    INT ret = 0;
    while( s <= e )
    {
        if( s%2 == 1 ) ret += Tree[s], s++;
        if( e%2 == 0 ) ret += Tree[e], e--;
        s>>=1, e>>=1;
    }
    return ret;
}
int main(){
    ios::sync_with_stdio(false);
    cin>>n>>m;
    for(int i = 0 ; i < n ; i++ )
    {
        INT x;
        cin>>x;
        V.insert(x);
        S.insert(x);
    }
    V.insert(1000000001);
    S.insert(1000000001);
    for(int i = 0 ; i < m ; i++ )
    {
        INT x, y, z;
        cin>>x>>y>>z;
        if( z == 1000000000 ) continue;
        H.push_back(make_pair(x, y));
        S.insert(x);
        S.insert(y);
        if( x == 1 and y == 1000000000 ) hole++;
    }
    if( n == 0 ) return cout<<hole, 0;
    sort(H.begin(), H.end());
    for(INT x : S ) mp[x] = val, ump[val] = x, val++;
    for( base = 1 ; base < val ; base<<=1 );
    for(int i = 0, j = 0, pre = 0, vc = 0 ; i < val ; i++ )
    {
        while( j < m and mp[H[j].first] <= i  )
        {
            Tree[mp[H[j].second] + base] += 1;
            update((mp[H[j].second]+base)>>1);
            j++;
        }
        if( V.find(ump[i]) != V.end() )
        {
            ans = min( ans, Tree[1] + vc );
            pre = i;
            vc++;
        }
        Tree[i+base] = 0, update((i+base)>>1);
    }
    cout<<ans;
    return 0;
}


#pragma GCC optimize("Ofast")

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef tree<
int,
null_type,
less_equal<int>,
rb_tree_tag,
tree_order_statistics_node_update> ordered_set;

#define INIT ios_base::sync_with_stdio(false);\
             cin.tie(0),cout.tie()
#define endl '\n'
#define fr first
#define sc second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define ins insert
#define ers erase
#define sz(c) ((int)(c).size())
#define all(x) (x).begin(),(x).end()
#define unique(x) (x).resize(unique(all(x))-(x).begin())
#define debug(_fmt,...) \
fprintf(stderr,"("#__VA_ARGS__ ") = (" _fmt")\n",__VA_ARGS__)

template<class T>
void na(vector<T>& a, int n)
{a = vector<T>(n);for(T& i: a) cin >> i;}
template<class T>
void printVector(vector<T>& a)
{for(T& i: a) cout << i << ' '; cout << endl;}
template<class T>
vector<T> shrink(vector<T>& x)
{
  vector<T> vals = x;
  sort(all(vals)); unique(vals);
  for(T& i: x) i = ub(all(vals), i) - vals.begin();
  return vals;
}

struct event
{
  int x, t; // t = 1 - Ih, 2 - V, 3 - Fh
  int ini;
  event(int x, int t, int ini=-1)
    : x(x), t(t), ini(ini){}
  bool operator< (const event& e)const
  {
    if(x != e.x) return x < e.x;
    return t < e.t;
  }
};
const int N = 1e6;
int ft[N];
void upd(int x, int v)
{
  for(;x<N;x+=x&-x)
    ft[x] += v;
}
int qry(int x)
{
  int res=0;
  for(;x>0 ;x-=x&-x)
    res += ft[x];
  return res;
}

int main()
{
  #ifdef OJUDGE
    //freopen("in","r",stdin);
  #endif
  //~ INIT;

  int n,m;
  scanf("%d %d", &n, &m);
  vector<event> events;
  bool is1e9 = false;
  vector<int> X;
  for(int i=1;i<=n;++i)
  {
    int x;
    scanf("%d", &x);
    X.pb(x);
    events.eb(x, 2);
    if(x == 1e9-1) is1e9 = true;
  }
  for(int i=1;i<=m;++i)
  {
    int x1, x2, y;
    scanf("%d %d %d", &x1, &x2, &y);
    X.pb(x1);
    X.pb(x2);
    events.eb(x1, 1);
    events.eb(x2, 3, x1);
  }
  vector<int> vals = shrink(X);

  if(!is1e9)events.eb(1e9-1,2);

  sort(all(events));
  int lastVer = 0, ans = INT_MAX, seen = 0;
  for(event& e: events)
  {
    //~ debug("%d %d", e.x, e.t);
    if(e.t == 1)
      upd(ub(all(vals), e.x) - vals.begin(),1)/*, cerr << "isnerted: "<< e.x << endl*/;
    if(e.t == 2)
    {
      int tmp = qry(lastVer + 1);
      if(seen)
        ans = min(ans, seen + tmp);
      else ans = min(ans, tmp);
      //~ cerr <<"lastVEr: " << lastVer << " hcut : " << tmp << endl;
      lastVer = e.x;
      ++seen;
    }
    if(e.t == 3)
      upd(ub(all(vals), e.ini) - vals.begin(),-1)/*, cerr << "erased: "<< e.ini << endl*/;
  }
  cout << ans << endl;
  return 0;
}


#pragma GCC optimize("Ofast")

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef tree<
int,
null_type,
less_equal<int>,
rb_tree_tag,
tree_order_statistics_node_update> ordered_set;

#define INIT ios_base::sync_with_stdio(false);\
             cin.tie(0),cout.tie()
#define endl '\n'
#define fr first
#define sc second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define ins insert
#define ers erase
#define sz(c) ((int)(c).size())
#define all(x) (x).begin(),(x).end()
#define unique(x) (x).resize(unique(all(x))-(x).begin())
#define debug(_fmt,...) \
fprintf(stderr,"("#__VA_ARGS__ ") = (" _fmt")\n",__VA_ARGS__)

template<class T>
void na(vector<T>& a, int n)
{a = vector<T>(n);for(T& i: a) cin >> i;}
template<class T>
void printVector(vector<T>& a)
{for(T& i: a) cout << i << ' '; cout << endl;}
template<class T>
vector<T> shrink(vector<T>& x)
{
  vector<T> vals = x;
  sort(all(vals)); unique(vals);
  for(T& i: x) i = ub(all(vals), i) - vals.begin();
  return vals;
}

struct event
{
  int x, t; // t = 1 - Ih, 2 - V, 3 - Fh
  int ini;
  event(int x, int t, int ini=-1)
    : x(x), t(t), ini(ini){}
  bool operator< (const event& e)const
  {
    if(x != e.x) return x < e.x;
    return t < e.t;
  }
};
const int N = 1e6;
int ft[N];
void upd(int x, int v)
{
  for(;x<N;x+=x&-x)
    ft[x] += v;
}
int qry(int x)
{
  int res=0;
  for(;x>0 ;x-=x&-x)
    res += ft[x];
  return res;
}

int main()
{
  #ifdef OJUDGE
    //freopen("in","r",stdin);
  #endif
  //~ INIT;

  int n,m;
  scanf("%d %d", &n, &m);
  vector<event> events;
  bool is1e9 = false;
  vector<int> X;
  for(int i=1;i<=n;++i)
  {
    int x;
    scanf("%d", &x);
    X.pb(x);
    events.eb(x, 2);
    if(x == 1e9-1) is1e9 = true;
  }
  for(int i=1;i<=m;++i)
  {
    int x1, x2, y;
    scanf("%d %d %d", &x1, &x2, &y);
    X.pb(x1);
    X.pb(x2);
    events.eb(x1, 1);
    events.eb(x2, 3, x1);
  }
  if(!is1e9)events.eb(1e9-1,2), X.pb(1e9-1);
  vector<int> vals = shrink(X);

  sort(all(events));
  int lastVer = 0, ans = INT_MAX, seen = 0;
  for(event& e: events)
  {
    //~ debug("%d %d", e.x, e.t);
    if(e.t == 1)
      upd(ub(all(vals), e.x) - vals.begin(),1)/*, cerr << "isnerted: "<< e.x << endl*/;
    if(e.t == 2)
    {
      int tmp = qry(lastVer + 1);
      if(seen)
        ans = min(ans, seen + tmp);
      else ans = min(ans, tmp);
      //~ cerr <<"lastVEr: " << lastVer << " hcut : " << tmp << endl;
      lastVer = e.x;
      ++seen;
    }
    if(e.t == 3)
      upd(ub(all(vals), e.ini) - vals.begin(),-1)/*, cerr << "erased: "<< e.ini << endl*/;
  }
  cout << ans << endl;
  return 0;
}


#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

#define INIT ios_base::sync_with_stdio(false);\
             cin.tie(0),cout.tie()
#define endl '\n'
#define fr first
#define sc second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define ins insert
#define ers erase
#define sz(c) ((int)(c).size())
#define all(x) (x).begin(),(x).end()
#define unique(x) (x).resize(unique(all(x))-(x).begin())
#define debug(_fmt,...) \
fprintf(stderr,"("#__VA_ARGS__ ") = (" _fmt")\n",__VA_ARGS__)

template<class T>
void na(vector<T>& a, int n)
{a = vector<T>(n);for(T& i: a) cin >> i;}
template<class T>
void printVector(vector<T>& a)
{for(T& i: a) cout << i << ' '; cout << endl;}
template<class T>
vector<T> shrink(vector<T>& x)
{
  vector<T> vals = x;
  sort(all(vals)); unique(vals);
  for(T& i: x) i = ub(all(vals), i) - vals.begin();
  return vals;
}

int main()
{
  #ifdef OJUDGE
    //freopen("in","r",stdin);
  #endif
  //~ INIT;

  int n,m;
  scanf("%d %d", &n, &m);
  vector<int> xH, xV(n);
  for(int& i: xV) scanf("%d", &i);
  sort(all(xV));
  for(int i=1;i<=m;++i)
  {
    int x1,x2,y;
    scanf("%d%d%d", &x1, &x2, &y);
    if(x1 != 1) continue;
    xH.pb(x2);
  }
  sort(all(xH));
  auto it = xH.begin();
  xV.pb(1e9);
  int ans = INT_MAX;
  for(int i=0;i<sz(xV);++i)
  {
    while(it != xH.end() && *it < xV[i]) it++;
    ans = min(ans, i + (int)distance(it, xH.end()));
  }

  cout << ans << endl;
  return 0;
}


#pragma GCC optimize("Ofast")

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef tree<
int,
null_type,
less_equal<int>,
rb_tree_tag,
tree_order_statistics_node_update> ordered_set;

#define INIT ios_base::sync_with_stdio(false);\
             cin.tie(0),cout.tie()
#define endl '\n'
#define fr first
#define sc second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define ins insert
#define ers erase
#define sz(c) ((int)(c).size())
#define all(x) (x).begin(),(x).end()
#define unique(x) (x).resize(unique(all(x))-(x).begin())
#define debug(_fmt,...) \
fprintf(stderr,"("#__VA_ARGS__ ") = (" _fmt")\n",__VA_ARGS__)

template<class T>
void na(vector<T>& a, int n)
{a = vector<T>(n);for(T& i: a) cin >> i;}
template<class T>
void printVector(vector<T>& a)
{for(T& i: a) cout << i << ' '; cout << endl;}
template<class T>
vector<T> shrink(vector<T>& x)
{
  vector<T> vals = x;
  sort(all(vals)); unique(vals);
  for(T& i: x) i = ub(all(vals), i) - vals.begin();
  return vals;
}

struct event
{
  int x, t; // t = 1 - Ih, 2 - V, 3 - Fh
  int ini;
  event(int x, int t, int ini=-1)
    : x(x), t(t), ini(ini){}
  bool operator< (const event& e)const
  {
    if(x != e.x) return x < e.x;
    return t < e.t;
  }
};
int N;
vector<int> ft;
void upd(int x, int v)
{
  for(;x<N;x+=x&-x)
    ft[x] += v;
}
int qry(int x)
{
  int res=0;
  for(;x>0 ;x-=x&-x)
    res += ft[x];
  return res;
}

int main()
{
  #ifdef OJUDGE
    //freopen("in","r",stdin);
  #endif
  //~ INIT;

  int n,m;
  scanf("%d %d", &n, &m);
  vector<event> events;
  bool is1e9 = false;
  vector<int> X;
  for(int i=1;i<=n;++i)
  {
    int x;
    scanf("%d", &x);
    X.pb(x);
    events.eb(x, 2);
    if(x == 1e9-1) is1e9 = true;
  }
  for(int i=1;i<=m;++i)
  {
    int x1, x2, y;
    scanf("%d %d %d", &x1, &x2, &y);
    X.pb(x1);
    X.pb(x2);
    events.eb(x1, 1);
    events.eb(x2, 3, x1);
  }
  X.pb(0);
  X.pb(1e9);
  //if(!is1e9)events.eb(1e9-1,2);
  vector<int> vals = shrink(X);
  ft = vector<int>(N = 2*(sz(vals)+1), 0);

  sort(all(events));
  int lastVer = 0, ans = INT_MAX, seen = 0;
  for(event& e: events)
  {
    //~ debug("%d %d", e.x, e.t);
    if(e.t == 1)
      upd(ub(all(vals), e.x) - vals.begin(),1)/*, cerr << "isnerted: "<< e.x << endl*/;
    if(e.t == 2)
    {
      int tmp = qry(ub(all(vals), lastVer) - vals.begin() + 1);
      if(seen)
        ans = min(ans, seen + tmp);
      else ans = min(ans, tmp);
      //~ cerr <<"lastVEr: " << lastVer << " hcut : " << tmp << endl;
      lastVer = e.x;
      ++seen;
    }
    if(e.t == 3)
      upd(ub(all(vals), e.ini) - vals.begin(),-1)/*, cerr << "erased: "<< e.ini << endl*/;
  }
  cout << ans << endl;
  return 0;
}


#pragma GCC optimize("Ofast")

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef tree<
int,
null_type,
less_equal<int>,
rb_tree_tag,
tree_order_statistics_node_update> ordered_set;

#define INIT ios_base::sync_with_stdio(false);\
             cin.tie(0),cout.tie()
#define endl '\n'
#define fr first
#define sc second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define ins insert
#define ers erase
#define sz(c) ((int)(c).size())
#define all(x) (x).begin(),(x).end()
#define unique(x) (x).resize(unique(all(x))-(x).begin())
#define debug(_fmt,...) \
fprintf(stderr,"("#__VA_ARGS__ ") = (" _fmt")\n",__VA_ARGS__)

template<class T>
void na(vector<T>& a, int n)
{a = vector<T>(n);for(T& i: a) cin >> i;}
template<class T>
void printVector(vector<T>& a)
{for(T& i: a) cout << i << ' '; cout << endl;}
template<class T>
vector<T> shrink(vector<T>& x)
{
  vector<T> vals = x;
  sort(all(vals)); unique(vals);
  for(T& i: x) i = ub(all(vals), i) - vals.begin();
  return vals;
}

struct event
{
  int x, t; // t = 1 - Ih, 2 - V, 3 - Fh
  int ini;
  event(int x, int t, int ini=-1)
    : x(x), t(t), ini(ini){}
  bool operator< (const event& e)const
  {
    if(x != e.x) return x < e.x;
    return t < e.t;
  }
};
const int N = 3e5+7;
int ft[N];
void upd(int x, int v)
{
  for(;x<N;x+=x&-x)
    ft[x] += v;
}
int qry(int x)
{
  int res=0;
  for(;x>0 ;x-=x&-x)
    res += ft[x];
  return res;
}

int main()
{
  #ifdef OJUDGE
    //freopen("in","r",stdin);
  #endif
  //~ INIT;

  int n,m;
  scanf("%d %d", &n, &m);
  vector<event> events;
  bool is1e9 = false;
  vector<int> X;
  for(int i=1;i<=n;++i)
  {
    int x;
    scanf("%d", &x);
    X.pb(x);
    events.eb(x, 2);
    if(x == 1e9-1) is1e9 = true;
  }
  for(int i=1;i<=m;++i)
  {
    int x1, x2, y;
    scanf("%d %d %d", &x1, &x2, &y);
    X.pb(x1);
    X.pb(x2);
    events.eb(x1, 1);
    events.eb(x2, 3, x1);
  }
  vector<int> vals = shrink(X);

  if(!is1e9)events.eb(1e9-1,2);

  sort(all(events));
  int lastVer = 0, ans = INT_MAX, seen = 0;
  for(event& e: events)
  {
    //~ debug("%d %d", e.x, e.t);
    if(e.t == 1)
      upd(ub(all(vals), e.x) - vals.begin(),1)/*, cerr << "isnerted: "<< e.x << endl*/;
    if(e.t == 2)
    {
      int tmp = qry(lastVer + 1);
      if(seen)
        ans = min(ans, seen + tmp);
      else ans = min(ans, tmp);
      //~ cerr <<"lastVEr: " << lastVer << " hcut : " << tmp << endl;
      lastVer = e.x;
      ++seen;
    }
    if(e.t == 3)
      upd(ub(all(vals), e.ini) - vals.begin(),-1)/*, cerr << "erased: "<< e.ini << endl*/;
  }
  cout << ans << endl;
  return 0;
}


#pragma GCC optimize("Ofast")

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef tree<
int,
null_type,
less_equal<int>,
rb_tree_tag,
tree_order_statistics_node_update> ordered_set;

#define INIT ios_base::sync_with_stdio(false);\
             cin.tie(0),cout.tie()
#define endl '\n'
#define fr first
#define sc second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define ins insert
#define ers erase
#define sz(c) ((int)(c).size())
#define all(x) (x).begin(),(x).end()
#define unique(x) (x).resize(unique(all(x))-(x).begin())
#define debug(_fmt,...) \
fprintf(stderr,"("#__VA_ARGS__ ") = (" _fmt")\n",__VA_ARGS__)

template<class T>
void na(vector<T>& a, int n)
{a = vector<T>(n);for(T& i: a) cin >> i;}
template<class T>
void printVector(vector<T>& a)
{for(T& i: a) cout << i << ' '; cout << endl;}
template<class T>
vector<T> shrink(vector<T>& x)
{
  vector<T> vals = x;
  sort(all(vals)); unique(vals);
  for(T& i: x) i = ub(all(vals), i) - vals.begin();
  return vals;
}

struct event
{
  int x, t; // t = 1 - Ih, 2 - V, 3 - Fh
  int ini;
  event(int x, int t, int ini=-1)
    : x(x), t(t), ini(ini){}
  bool operator< (const event& e)const
  {
    if(x != e.x) return x < e.x;
    return t < e.t;
  }
};
int N;
vector<int> ft;
void upd(int x, int v)
{
  for(;x<N;x+=x&-x)
    ft[x] += v;
}
int qry(int x)
{
  int res=0;
  for(;x>0 ;x-=x&-x)
    res += ft[x];
  return res;
}

int main()
{
  #ifdef OJUDGE
    //freopen("in","r",stdin);
  #endif
  //~ INIT;

  int n,m;
  scanf("%d %d", &n, &m);
  vector<event> events;
  bool is1e9 = false;
  vector<int> X;
  for(int i=1;i<=n;++i)
  {
    int x;
    scanf("%d", &x);
    X.pb(x);
    events.eb(x, 2);
    if(x == 1e9-1) is1e9 = true;
  }
  for(int i=1;i<=m;++i)
  {
    int x1, x2, y;
    scanf("%d %d %d", &x1, &x2, &y);
    X.pb(x1);
    X.pb(x2);
    events.eb(x1, 1);
    events.eb(x2, 3, x1);
  }
  X.pb(0);
  X.pb(1e9-1);
  if(!is1e9)events.eb(1e9-1,2);
  vector<int> vals = shrink(X);
  ft = vector<int>(N = 2*(sz(vals)+1), 0);

  sort(all(events));
  int lastVer = 0, ans = INT_MAX, seen = 0;
  for(event& e: events)
  {
    //~ debug("%d %d", e.x, e.t);
    if(e.t == 1)
      upd(ub(all(vals), e.x) - vals.begin(),1)/*, cerr << "isnerted: "<< e.x << endl*/;
    if(e.t == 2)
    {
      int tmp = qry(ub(all(vals), lastVer) - vals.begin() + 1);
      if(seen)
        ans = min(ans, seen + tmp);
      else ans = min(ans, tmp);
      //~ cerr <<"lastVEr: " << lastVer << " hcut : " << tmp << endl;
      lastVer = e.x;
      ++seen;
    }
    if(e.t == 3)
      upd(ub(all(vals), e.ini) - vals.begin(),-1)/*, cerr << "erased: "<< e.ini << endl*/;
  }
  cout << ans << endl;
  return 0;
}


#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <memory.h>
#include <deque>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef long long ll;
#define mp make_pair
const int M=100005;
const int N=1000001;
const int K=1000000000;
int n,m;
vector<int>v;
int main(){
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    #endif
    cin>>n>>m;
    for(int i=0,x;i<n;i++)
    {
        scanf("%d",&x);
        v.push_back(x);
    }
    v.push_back(1);
    v.push_back(K);
    v.push_back(K+1);
    sort(v.begin(),v.end());
    v.resize(unique(v.begin(),v.end())-v.begin());
    vector<int>u;
    u.resize(v.size()-1);
    for(int i=0,x,y,r;i<m;i++)
    {
        scanf("%d%d%d",&x,&y,&r);
        int j=lower_bound(v.begin(),v.end(),y)-v.begin();
        if(y==v[j])
            j++;
        //cout<<j-1<<endl;
        u[j-1]++;
    }
    int ans=K;
    int ct=0;
    for(int i=0;i<u.size()-1;i++)
    {
        ct+=u[i];
        ans=min(ans,m-ct+i);
    }
    cout<<ans;
}


#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <memory.h>
#include <deque>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef long long ll;
#define mp make_pair
const int M=100005;
const int N=1000001;
const int K=1000000000;
int n,m;
vector<int>v;
int main(){
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    #endif
    cin>>n>>m;
    for(int i=0,x;i<n;i++)
    {
        scanf("%d",&x);
        v.push_back(x);
    }
    v.push_back(0);
    v.push_back(K);
    v.push_back(K+1);
    sort(v.begin(),v.end());
    v.resize(unique(v.begin(),v.end())-v.begin());
    vector<int>u;
    u.resize(v.size()-1);
    int lel=0;
    for(int i=0,x,y,r;i<m;i++)
    {
        scanf("%d%d%d",&x,&y,&r);
        if(x!=1)
            continue;
        lel++;
        int j=lower_bound(v.begin(),v.end(),y)-v.begin();
        if(y==v[j])
            j++;
        //cout<<j-1<<endl;
        u[j-1]++;
    }
    int ans=K;
    int ct=0;
    for(int i=0;i<u.size()-1;i++)
    {
        ct+=u[i];
        ans=min(ans,lel-ct+i);
    }
    cout<<ans;
}


#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <memory.h>
#include <deque>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef long long ll;
#define mp make_pair
const int M=100005;
const int N=1000001;
const int K=1000000000;
int n,m;
vector<int>v;
int main(){
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    #endif
    cin>>n>>m;
    for(int i=0,x;i<n;i++)
    {
        scanf("%d",&x);
        v.push_back(x);
    }
    v.push_back(1);
    v.push_back(K);
    v.push_back(K+1);
    sort(v.begin(),v.end());
    v.resize(unique(v.begin(),v.end())-v.begin());
    vector<int>u;
    u.resize(v.size()-1);
    int lel=0;
    for(int i=0,x,y,r;i<m;i++)
    {
        scanf("%d%d%d",&x,&y,&r);
        if(x!=1)
            continue;
        lel++;
        int j=lower_bound(v.begin(),v.end(),y)-v.begin();
        if(y==v[j])
            j++;
        //cout<<j-1<<endl;
        u[j-1]++;
    }
    int ans=K;
    int ct=0;
    for(int i=0;i<u.size()-1;i++)
    {
        ct+=u[i];
        ans=min(ans,lel-ct+i);
    }
    cout<<ans;
}


#include <bits/stdc++.h>
using namespace std;

const int N = 100010;

int Vr[N], Hz[N], Q[N];

int main() {
        ios_base :: sync_with_stdio(0); cin.tie(0);cout.tie(0);
        int n, m; cin >> n >> m;
        for(int  i = 1; i <= n; i++) {
                 cin >> Vr[i];
        }
        sort(Vr + 1, Vr + n + 1);
        int cnt = 0, init = 0;
        for(int i  = 1; i <= m; i++) {
                int x1, x2, y; cin >> x1 >> x2 >> y;
                cnt += (x1 == 1 && x2 == 1000000000);
                if(x1 == 1) {
                        init += (Vr[1] == 1);
                        int U = upper_bound(Vr + 1, Vr + n + 1, x2) - Vr;
                        Q[0]++;
                        Q[U]--;
                }
        }
        for(int i = 1; i <= n; i++) {
                Q[i] += Q[i - 1];
        }
        Q[n + 1] = 0;
        int MX = INT_MAX;
        for(int i = 1; i <= n + 1; i++) {
                MX = min(MX, i - 1 + Q[i]);
        }
        cout << init + (n ? MX : cnt) << endl;

}


#include <bits/stdc++.h>
using namespace std;

const int N = 100010;

int Vr[N], Hz[N], Q[N];

int main() {
        ios_base :: sync_with_stdio(0); cin.tie(0);cout.tie(0);
        int n, m; cin >> n >> m;
        for(int  i = 1; i <= n; i++) {
                 cin >> Vr[i];
        }
        sort(Vr + 1, Vr + n + 1);
        int cnt = 0;
        for(int i  = 1; i <= m; i++) {
                int x1, x2, y; cin >> x1 >> x2 >> y;
                cnt += (x1 == 1 && x2 == 1000000000);
                if(x1 == 1) {
                        int U = upper_bound(Vr + 1, Vr + n + 1, x2) - Vr;
                        Q[0]++;
                        Q[U]--;
                }
        }
        for(int i = 1; i <= n; i++) {
                Q[i] += Q[i - 1];
        }
        int MX = INT_MAX;
        for(int i = 1; i <= n; i++) {
                MX = min(MX, i - 1 + Q[i]);
        }
        cout << (n ? MX : cnt) << endl;

}


#include <bits/stdc++.h>
using namespace std;

const int N = 100010;

int Vr[N], Hz[N], Q[N];

int main() {
        ios_base :: sync_with_stdio(0); cin.tie(0);cout.tie(0);
        int n, m; cin >> n >> m;
        for(int  i = 1; i <= n; i++) {
                 cin >> Vr[i];
        }
        sort(Vr + 1, Vr + n + 1);
        int cnt = 0;
        for(int i  = 1; i <= m; i++) {
                int x1, x2, y; cin >> x1 >> x2 >> y;
                cnt += (x1 == 1 && x2 == 1000000000);
                if(x1 == 1) {
                        int U = upper_bound(Vr + 1, Vr + n + 1, x2) - Vr;
                        Q[0]++;
                        Q[U]--;
                }
        }
        for(int i = 1; i <= n; i++) {
                Q[i] += Q[i - 1];
        }
        Q[n + 1] = 0;
        int MX = INT_MAX;
        for(int i = 1; i <= n + 1; i++) {
                MX = min(MX, i - 1 + Q[i]);
        }
        cout << (n ? MX : cnt) << endl;

}


#include <bits/stdc++.h>
using namespace std;

const int N = 100010;

int Vr[N], Hz[N], Q[N];

int main() {
        ios_base :: sync_with_stdio(0); cin.tie(0);cout.tie(0);
        int n, m; cin >> n >> m;
        for(int  i = 1; i <= n; i++) {
                 cin >> Vr[i];
        }
        sort(Vr + 1, Vr + n + 1);
        int cnt = 0;
        for(int i  = 1; i <= m; i++) {
                int x1, x2, y; cin >> x1 >> x2 >> y;
                if(x1 == 1 && x2 == 1000000000 && y == 1000000000) continue;
                cnt += (x1 == 1 && x2 == 1000000000);
                if(x1 == 1) {
                        int U = upper_bound(Vr + 1, Vr + n + 1, x2) - Vr;
                        Q[0]++;
                        Q[U]--;
                }
        }
        for(int i = 1; i <= n; i++) {
                Q[i] += Q[i - 1];
        }
        Q[n + 1] = 0;
        int MX = INT_MAX;
        for(int i = 1; i <= n; i++) {
                MX = min(MX, i - 1 + Q[i]);
        }
        MX = min(MX, cnt + n);
        cout << MX << endl;

}


#include <bits/stdc++.h>
using namespace std;
int n, m, i, j, p;
int main() {
    std::ios::sync_with_stdio(false);
    cin >> n >> m;
    vector<int>v(n);
    vector<int>h;
    int minival = -1;
    for (i = 0; i < n; i++) cin >> v[i];
    v.push_back(1000000000);
    int m1 = 0;
    for (j = 0; j < m; j++) {
        int a, b, c;
        cin >> a >> b >> c;
        if (a == 1) {
            h.push_back(b);
            m1++;
        }
    }
    m = m1;
    sort(v.begin(), v.end());
    sort(h.begin(), h.end());
    //for (int i = 0; i <= n; i++) cout << v[i] << " " ;
    //cout << endl;
    //for (int j = 0; j < m; j++) cout << h[j] << " ";
    //cout << endl;
    for (i=p=0; i <= n; i++) {
        for (;p<m && h[p]<v[i];p++);
        //int p = lower_bound(h.begin(), h.end(), v[i]) - h.begin();
        //cout << p[i] << " " << i+m-p[i];
        //cout << endl;
        if (minival == -1) minival = i+m-p;
        else minival = min(minival, i+m-p);
    }
    cout << minival << endl;
}


#include <bits/stdc++.h>
using namespace std;
int n, m;
int main() {
    std::ios::sync_with_stdio(false);
    cin >> n >> m;
    vector<long long>v(n);
    vector<long long>h;
    int minival = -1;
    for (int i = 0; i < n; i++) cin >> v[i];
    long long e = 1000000000;
    v.push_back(e);
    int m1 = 0;
    for (int j = 0; j < m; j++) {
        long long a, b, c;
        cin >> a >> b >> c;
        if (a == 1) {
            h.push_back(b);
            m1++;
        }
    }
    m = m1;
    sort(v.begin(), v.end());
    sort(h.begin(), h.end());
    //for (int i = 0; i <= n; i++) cout << v[i] << " " ;
    //cout << endl;
    //for (int j = 0; j < m; j++) cout << h[j] << " ";
    //cout << endl;
    int p;
    for (int i = 0; i <= n; i++) {
        p = 0;
        for (;p<m && h[p]<v[i];p++);
        //int p = lower_bound(h.begin(), h.end(), v[i]) - h.begin();
        //cout << p[i] << " " << i+m-p[i];
        //cout << endl;
        if (minival == -1) minival = i+m-p;
        else minival = min(minival, i+m-p);
    }
    cout << minival << endl;
}


#include <bits/stdc++.h>
using namespace std;
int n, m;
int p[100001];
int main() {
    std::ios::sync_with_stdio(false);
    cin >> n >> m;
    vector<long long>v(n);
    vector<long long>h;
    int minival = -1;
    for (int i = 0; i < n; i++) cin >> v[i];
    long long e = 1000000000;
    v.push_back(e);
    for (int j = 0; j < m; j++) {
        long long a, b, c;
        cin >> a >> b >> c;
        if (a == 1) h.push_back(b);
    }
    sort(v.begin(), v.end());
    sort(h.begin(), h.end());
    //for (int i = 0; i <= n; i++) cout << v[i] << " " ;
    //cout << endl;
    //for (int j = 0; j < m; j++) cout << h[j] << " ";
    //cout << endl;
    for (int i = 0; i <= n; i++) {
        p[i] = lower_bound(h.begin(), h.end(), v[i]) - h.begin();
        //cout << p[i] << " " << i+m-p[i];
        //cout << endl;
        if (minival == -1) minival = i+m-p[i];
        else minival = min(minival, i+m-p[i]);
    }
    cout << minival << endl;
}


#include <bits/stdc++.h>
using namespace std;
int n, m;
int p[100001];
int main() {
    std::ios::sync_with_stdio(false)
    cin >> n >> m;
    vector<long long>v(n);
    vector<long long>h;
    int minival = -1;
    for (int i = 0; i < n; i++) cin >> v[i];
    long long e = 1000000000;
    v.push_back(e);
    for (int j = 0; j < m; j++) {
        long long a, b, c;
        cin >> a >> b >> c;
        if (a == 1) h.push_back(b);
    }
    m = h.size();
    sort(v.begin(), v.end());
    sort(h.begin(), h.end());
    //for (int i = 0; i <= n; i++) cout << v[i] << " " ;
    //cout << endl;
    //for (int j = 0; j < m; j++) cout << h[j] << " ";
    //cout << endl;
    for (int i = 0; i <= n; i++) {
        p[i] = lower_bound(h.begin(), h.end(), v[i]) - h.begin();
        //cout << p[i] << " " << i+m-p[i];
        //cout << endl;
        if (minival == -1) minival = i+m-p[i];
        else minival = min(minival, i+m-p[i]);
    }
    cout << minival << endl;
}


#include <bits/stdc++.h>
using namespace std;
int n, m, i, j, p;
int main() {
    std::ios::sync_with_stdio(false);
    cin >> n >> m;
    vector<long long>v(n);
    vector<long long>h;
    int minival = -1;
    for (i = 0; i < n; i++) cin >> v[i];
    long long e = 1000000000;
    v.push_back(e);
    int m1 = 0;
    for (j = 0; j < m; j++) {
        long long a, b, c;
        cin >> a >> b >> c;
        if (a == 1) {
            h.push_back(b);
            m1++;
        }
    }
    m = m1;
    sort(v.begin(), v.end());
    sort(h.begin(), h.end());
    //for (int i = 0; i <= n; i++) cout << v[i] << " " ;
    //cout << endl;
    //for (int j = 0; j < m; j++) cout << h[j] << " ";
    //cout << endl;
    for (i=p=0; i <= n; i++) {
        for (;p<m && h[p]<v[i];p++);
        //int p = lower_bound(h.begin(), h.end(), v[i]) - h.begin();
        //cout << p[i] << " " << i+m-p[i];
        //cout << endl;
        if (minival == -1) minival = i+m-p;
        else minival = min(minival, i+m-p);
    }
    cout << minival << endl;
}


#include <bits/stdc++.h>
using namespace std;
int n, m;
int main() {
    std::ios::sync_with_stdio(false);
    cin >> n >> m;
    vector<long long>v(n);
    vector<long long>h;
    int minival = -1;
    for (int i = 0; i < n; i++) cin >> v[i];
    long long e = 1000000000;
    v.push_back(e);
    int m1 = 0;
    for (int j = 0; j < m; j++) {
        long long a, b, c;
        cin >> a >> b >> c;
        if (a == 1) {
            h.push_back(b);
            m1++;
        }
    }
    m = m1;
    sort(v.begin(), v.end());
    sort(h.begin(), h.end());
    //for (int i = 0; i <= n; i++) cout << v[i] << " " ;
    //cout << endl;
    //for (int j = 0; j < m; j++) cout << h[j] << " ";
    //cout << endl;
    int p;
    for (int i = 0; i <= n; i++) {
        p = 0;
        for (;h[p]<v[i];p++);
        //int p = lower_bound(h.begin(), h.end(), v[i]) - h.begin();
        //cout << p[i] << " " << i+m-p[i];
        //cout << endl;
        if (minival == -1) minival = i+m-p;
        else minival = min(minival, i+m-p);
    }
    cout << minival << endl;
}


#include <bits/stdc++.h>
using namespace std;
int n, m;
int p[100001];
int main() {
    cin >> n >> m;
    vector<long long>v(n);
    vector<long long>h;
    int minival = -1;
    for (int i = 0; i < n; i++) cin >> v[i];
    long long e = 1000000000;
    v.push_back(e);
    for (int j = 0; j < m; j++) {
        long long a, b, c;
        cin >> a >> b >> c;
        if (a == 1) h.push_back(b);
    }
    m = h.size();
    sort(v.begin(), v.end());
    sort(h.begin(), h.end());
    //for (int i = 0; i <= n; i++) cout << v[i] << " " ;
    //cout << endl;
    //for (int j = 0; j < m; j++) cout << h[j] << " ";
    //cout << endl;
    for (int i = 0; i <= n; i++) {
        p[i] = lower_bound(h.begin(), h.end(), v[i]) - h.begin();
        //cout << p[i] << " " << i+m-p[i];
        //cout << endl;
        if (minival == -1) minival = i+m-p[i];
        else minival = min(minival, i+m-p[i]);
    }
    cout << minival << endl;
}


#include <bits/stdc++.h>
using namespace std;
int n, m;
int main() {
    std::ios::sync_with_stdio(false);
    cin >> n >> m;
    vector<long long>v(n);
    vector<long long>h;
    int minival = -1;
    for (int i = 0; i < n; i++) cin >> v[i];
    long long e = 1000000000;
    v.push_back(e);
    int m1 = 0;
    for (int j = 0; j < m; j++) {
        long long a, b, c;
        cin >> a >> b >> c;
        if (a == 1) {
            h.push_back(b);
            m1++;
        }
    }
    m = m1;
    sort(v.begin(), v.end());
    sort(h.begin(), h.end());
    //for (int i = 0; i <= n; i++) cout << v[i] << " " ;
    //cout << endl;
    //for (int j = 0; j < m; j++) cout << h[j] << " ";
    //cout << endl;
    int p;
    for (int i = 0; i <= n; i++) {
        p = 0;
        for (;h[p]<v[i] && p<m;p++);
        //int p = lower_bound(h.begin(), h.end(), v[i]) - h.begin();
        //cout << p[i] << " " << i+m-p[i];
        //cout << endl;
        if (minival == -1) minival = i+m-p;
        else minival = min(minival, i+m-p);
    }
    cout << minival << endl;
}


 #include <bits/stdc++.h>
using namespace std;
int n, m;
int p[100001];
int main() {
    std::ios::sync_with_stdio(false);
    cin >> n >> m;
    vector<long long>v(n);
    vector<long long>h;
    int minival = -1;
    for (int i = 0; i < n; i++) cin >> v[i];
    long long e = 1000000000;
    v.push_back(e);
    int m1 = 0;
    for (int j = 0; j < m; j++) {
        long long a, b, c;
        cin >> a >> b >> c;
        if (a == 1) {
            h.push_back(b);
            m1++;
        }
    }
    m = m1;
    sort(v.begin(), v.end());
    sort(h.begin(), h.end());
    //for (int i = 0; i <= n; i++) cout << v[i] << " " ;
    //cout << endl;
    //for (int j = 0; j < m; j++) cout << h[j] << " ";
    //cout << endl;
    for (int i = 0; i <= n; i++) {
        p[i] = lower_bound(h.begin(), h.end(), v[i]) - h.begin();
        //cout << p[i] << " " << i+m-p[i];
        //cout << endl;
        if (minival == -1) minival = i+m-p[i];
        else minival = min(minival, i+m-p[i]);
    }
    cout << minival << endl;
}


 #include <bits/stdc++.h>
using namespace std;
int n, m;
int p[100001];
int main() {
    std::ios::sync_with_stdio(false);
    cin >> n >> m;
    vector<long long>v(n);
    vector<long long>h;
    int minival = -1;
    for (int i = 0; i < n; i++) cin >> v[i];
    long long e = 1000000000;
    v.push_back(e);
    for (int j = 0; j < m; j++) {
        long long a, b, c;
        cin >> a >> b >> c;
        if (a == 1) h.push_back(b);
    }
    m = h.size();
    sort(v.begin(), v.end());
    sort(h.begin(), h.end());
    //for (int i = 0; i <= n; i++) cout << v[i] << " " ;
    //cout << endl;
    //for (int j = 0; j < m; j++) cout << h[j] << " ";
    //cout << endl;
    for (int i = 0; i <= n; i++) {
        p[i] = lower_bound(h.begin(), h.end(), v[i]) - h.begin();
        //cout << p[i] << " " << i+m-p[i];
        //cout << endl;
        if (minival == -1) minival = i+m-p[i];
        else minival = min(minival, i+m-p[i]);
    }
    cout << minival << endl;
}


 #include <bits/stdc++.h>
using namespace std;
int n, m;
int main() {
    std::ios::sync_with_stdio(false);
    cin >> n >> m;
    vector<long long>v(n);
    vector<long long>h;
    int minival = -1;
    for (int i = 0; i < n; i++) cin >> v[i];
    long long e = 1000000000;
    v.push_back(e);
    int m1 = 0;
    for (int j = 0; j < m; j++) {
        long long a, b, c;
        cin >> a >> b >> c;
        if (a == 1) {
            h.push_back(b);
            m1++;
        }
    }
    m = m1;
    sort(v.begin(), v.end());
    sort(h.begin(), h.end());
    //for (int i = 0; i <= n; i++) cout << v[i] << " " ;
    //cout << endl;
    //for (int j = 0; j < m; j++) cout << h[j] << " ";
    //cout << endl;
    for (int i=p=0; i <= n; i++) {
        for (;p<m && h[p]<v[i];p++);
        //int p = lower_bound(h.begin(), h.end(), v[i]) - h.begin();
        //cout << p[i] << " " << i+m-p[i];
        //cout << endl;
        if (minival == -1) minival = i+m-p;
        else minival = min(minival, i+m-p);
    }
    cout << minival << endl;
}