#pragma GCC optimize("O3")
#ifdef ONLINE_JUDGE
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#endif
#pragma GCC optimize("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#include <bits/stdc++.h>
#include <deque>
#include <type_traits>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
template<typename KeyType=int,typename Mapped=__gnu_pbds::null_type,typename Cmp_Fn=std::less<KeyType>,typename Tag=__gnu_pbds::rb_tree_tag,template<typename Const_Node_Iterator,typename Node_Iterator,typename Cmp_Fn_,typename Allocator_>class Node_Update=__gnu_pbds::tree_order_statistics_node_update,typename Allocator=std::allocator<char>>using ordered_set_t=__gnu_pbds::tree<KeyType,Mapped,Cmp_Fn,Tag,Node_Update,Allocator>;const int GRANDOM=std::chrono::high_resolution_clock::now().time_since_epoch().count();struct ghash{int operator()(int x){return std::hash<int>{}(x^GRANDOM);}};template<typename KeyType>using hash_table_t=__gnu_pbds::gp_hash_table<KeyType,int,ghash>;namespace vh{template<class T>struct like_array:is_array<T>{};template<class T,size_t N>struct like_array<array<T,N>>:true_type{};template<class T>struct like_array<vector<T>>:true_type{};template<class T>bool is_like_array(const T&a){return like_array<T>::value;}template<class T>void _R(T&x){std::cin>>x;}inline void _R(int&x){scanf("%d",&x);}inline void _R(int64_t&x){scanf("%" SCNd64,&x);}inline void _R(double&x){scanf("%lf",&x);}inline void _R(char&x){scanf(" %c",&x);}inline void _R(char*x){scanf("%s",x);}template<typename T>inline void _R(vector<T>&v,size_t ie){for(int i=0;i<ie;i++)_R(v[i]);}template<typename T>inline void _R(vector<T>&v){_R(v,v.size());}inline void R(){}template<class T,class... U>inline void R(T&head,U&... tail){_R(head);R(tail...);}template<class T>void _W(const T&x){cout<<x;}template<typename T>inline void _W(vector<T>&v,size_t ie){for(int i=0;i<ie;i++){if(i>0)_W(' ');_W(v[i]);}_W('\n');}template<typename T>inline void _W(vector<T>&v){_W(v,v.size());}template<class T>inline void _W(const vector<T>&x){for(auto i=x.begin();i!=x.end();_W(*i++))if(i!=x.cbegin())putchar(' ');}inline void W(){}template<class T,class... U>inline void W(const T&head,const U&... tail){_W(head);if(sizeof...(tail))putchar(' '),W(tail...);}template<class T,class... U>inline void WL(const T&head,const U&... tail){_W(head);putchar(sizeof...(tail)? ' ':'\n');W(tail...);}template<class T>void _RE(T&x){cin>>x;}template<class Arg,class... Args>void _RE(Arg&first,Args&... rest);void _RE(double&x){string t;_RE(t);x=stod(t);}void _RE(long double&x){string t;_RE(t);x=stold(t);}template<class T>void _RE(complex<T>&x);template<class T1,class T2>void _RE(pair<T1,T2>&p);template<class T>void _RE(vector<T>&a);template<class T,size_t SZ>void _RE(array<T,SZ>&a);template<class Arg,class... Args>void _RE(Arg&first,Args&... rest){_RE(first);_RE(rest...);}template<class T>void _RE(complex<T>&x){T a,b;_RE(a,b);x=cd(a,b);}template<class T1,class T2>void _RE(pair<T1,T2>&p){_RE(p.f,p.s);}template<class T>void _RE(vector<T>&a){for(int i=0,ie=sz(a);i<ie;i++)_RE(a[i]);}template<class T,size_t SZ>void _RE(array<T,SZ>&a){for(int i=0;i<SZ;i++)_RE(a[i]);}};
#define prec setprecision
namespace vh{typedef long long ll;typedef unsigned long long llu;template<typename T>T gcd(T m,T n){while(n){T t=m%n;m=n;n=t;};return m;}template<typename T>T exgcd(T a,T b,T&sa,T&ta){T q,r,sb=0,tb=1,sc,tc;sa=1,ta=0;if(b)do q=a/b,r=a-q*b,a=b,b=r,sc=sa-q*sb,sa=sb,sb=sc,tc=ta-q*tb,ta=tb,tb=tc;while(b);return a;}template<typename T>T mul_inv(T a,T b){T t1=a,t2=b,t3;T v1=1,v2=0,v3;T x;while(t2!=1)x=t1/t2,t3=t1-x*t2,v3=v1-x*v2,t1=t2,t2=t3,v1=v2,v2=v3;return(v2+b)%b;}template<typename T>T powmod(T a,T b,T MOD){if(b<0)return 0;T rv=1;while(b)(b%2)&&(rv=(rv*a)%MOD),a=a*a%MOD,b/=2;return rv;}template<typename T>inline T isqrt(T k){T r=sqrt((double)k)+1;while(r*r>k)r--;return r;}template<typename T>inline T icbrt(T k){T r=cbrt((double)k)+1;while(r*r*r>k)r--;return r;}template<typename T>bool mul_overflow(T&r,T a,T b){return __builtin_mul_overflow(a,b,&r);}template<ll n>struct BitSize{enum{Size=BitSize<n/2>::Size+1};};template<>struct BitSize<0>{enum{Size=1};};template<>struct BitSize<1>{enum{Size=1};};
#define BITSIZE(n) (BitSize<n>::Size)
#define TREESIZE(n) ((1<<(BitSize<n>::Size + 1))+7)
#define BITMAX(n) (BitSize<n>::Size - 1)
#define DEBUG !defined(ONLINE_JUDGE)
#define SZ(x) ((int)(x).size())
#define ALL(x) begin(x), end(x)
#define RALL(x) (x).rbegin(),(x).rend()
template<typename TH>void _dbg(const char*sdbg,TH h){cerr<<sdbg<<"="<<h<<"\n";}template<typename TH,typename... TA>void _dbg(const char*sdbg,TH h,TA... t){while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<",";_dbg(sdbg+1,t...);}
#ifdef DEBUG
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; for(auto itt: x) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif
}namespace vh{template<typename T>int min_rotation(vector<T>s){int a=0,N=s.size();for(int b=0;b<N;b++)for(int i=0;i<N;i++){if(a+i==b||s[(a+i)%N]<s[(b+i)%N]){b+=max(0,i-1);break;}if(s[(a+i)%N]>s[(b+i)%N]){a=b;break;}}return a;};template<typename T>class StackGuard{private:T x;public:StackGuard(T x):x(x){}~StackGuard(){x();}};}namespace vh{};
namespace vh{
  const int maxn=200200;
  int n,q;
  int a[maxn];
  int mi[maxn],ma[maxn];
  int cnt[maxn];
  int main(){
    srand(GRANDOM);
    cin>>n>>q;
    memset(mi,-1,sizeof(mi));
    memset(ma,-1,sizeof(ma));
    memset(cnt,0,sizeof(cnt));
    set<int>v;
    for(int i=0;i<n;i++){
      cin>>a[i];
      v.insert(a[i]);
      cnt[a[i]]++;
      if(mi[a[i]]<0)mi[a[i]]=ma[a[i]]=i;
      else ma[a[i]]=i;
    }
    vector<int>vi(v.begin(),v.end());
    sort(vi.begin(),vi.end(),[](int x,int y){
      if(mi[x]!=mi[y])return mi[x]<mi[y];
      if(ma[x]!=ma[y])return ma[x]>ma[y];
      return false;
    });
    int currma=-1;
    int ans=0;
    set<pair<int,int>>ss;
    vi.push_back(maxn-1);
    mi[maxn-1]=maxn;
    for(auto x:vi){
      if(mi[x]>currma){
        currma=ma[x];
        if(!ss.empty()){
          ss.erase(prev(ss.end()));
          for(auto [c,_]:ss)ans+=c;
          ss.clear();
        }
        ss.insert({cnt[x],x});
      }else{
        ss.insert({cnt[x],x});
        currma=max(currma,ma[x]);
      }
    }
    cout<<ans<<endl;
    return 0;
  }
};
int main(int argc,char*argv[]){
  std::cin.sync_with_stdio(false);std::cin.tie(nullptr);
  return vh::main();
}


#pragma GCC optimize("O3")
#ifdef ONLINE_JUDGE
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#endif
#pragma GCC optimize("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#include <bits/stdc++.h>
#include <deque>
#include <type_traits>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
template<typename KeyType=int,typename Mapped=__gnu_pbds::null_type,typename Cmp_Fn=std::less<KeyType>,typename Tag=__gnu_pbds::rb_tree_tag,template<typename Const_Node_Iterator,typename Node_Iterator,typename Cmp_Fn_,typename Allocator_>class Node_Update=__gnu_pbds::tree_order_statistics_node_update,typename Allocator=std::allocator<char>>using ordered_set_t=__gnu_pbds::tree<KeyType,Mapped,Cmp_Fn,Tag,Node_Update,Allocator>;const int GRANDOM=std::chrono::high_resolution_clock::now().time_since_epoch().count();struct ghash{int operator()(int x){return std::hash<int>{}(x^GRANDOM);}};template<typename KeyType>using hash_table_t=__gnu_pbds::gp_hash_table<KeyType,int,ghash>;namespace vh{template<class T>struct like_array:is_array<T>{};template<class T,size_t N>struct like_array<array<T,N>>:true_type{};template<class T>struct like_array<vector<T>>:true_type{};template<class T>bool is_like_array(const T&a){return like_array<T>::value;}template<class T>void _R(T&x){std::cin>>x;}inline void _R(int&x){scanf("%d",&x);}inline void _R(int64_t&x){scanf("%" SCNd64,&x);}inline void _R(double&x){scanf("%lf",&x);}inline void _R(char&x){scanf(" %c",&x);}inline void _R(char*x){scanf("%s",x);}template<typename T>inline void _R(vector<T>&v,size_t ie){for(int i=0;i<ie;i++)_R(v[i]);}template<typename T>inline void _R(vector<T>&v){_R(v,v.size());}inline void R(){}template<class T,class... U>inline void R(T&head,U&... tail){_R(head);R(tail...);}template<class T>void _W(const T&x){cout<<x;}template<typename T>inline void _W(vector<T>&v,size_t ie){for(int i=0;i<ie;i++){if(i>0)_W(' ');_W(v[i]);}_W('\n');}template<typename T>inline void _W(vector<T>&v){_W(v,v.size());}template<class T>inline void _W(const vector<T>&x){for(auto i=x.begin();i!=x.end();_W(*i++))if(i!=x.cbegin())putchar(' ');}inline void W(){}template<class T,class... U>inline void W(const T&head,const U&... tail){_W(head);if(sizeof...(tail))putchar(' '),W(tail...);}template<class T,class... U>inline void WL(const T&head,const U&... tail){_W(head);putchar(sizeof...(tail)? ' ':'\n');W(tail...);}template<class T>void _RE(T&x){cin>>x;}template<class Arg,class... Args>void _RE(Arg&first,Args&... rest);void _RE(double&x){string t;_RE(t);x=stod(t);}void _RE(long double&x){string t;_RE(t);x=stold(t);}template<class T>void _RE(complex<T>&x);template<class T1,class T2>void _RE(pair<T1,T2>&p);template<class T>void _RE(vector<T>&a);template<class T,size_t SZ>void _RE(array<T,SZ>&a);template<class Arg,class... Args>void _RE(Arg&first,Args&... rest){_RE(first);_RE(rest...);}template<class T>void _RE(complex<T>&x){T a,b;_RE(a,b);x=cd(a,b);}template<class T1,class T2>void _RE(pair<T1,T2>&p){_RE(p.f,p.s);}template<class T>void _RE(vector<T>&a){for(int i=0,ie=sz(a);i<ie;i++)_RE(a[i]);}template<class T,size_t SZ>void _RE(array<T,SZ>&a){for(int i=0;i<SZ;i++)_RE(a[i]);}};
#define prec setprecision
namespace vh{typedef long long ll;typedef unsigned long long llu;template<typename T>T gcd(T m,T n){while(n){T t=m%n;m=n;n=t;};return m;}template<typename T>T exgcd(T a,T b,T&sa,T&ta){T q,r,sb=0,tb=1,sc,tc;sa=1,ta=0;if(b)do q=a/b,r=a-q*b,a=b,b=r,sc=sa-q*sb,sa=sb,sb=sc,tc=ta-q*tb,ta=tb,tb=tc;while(b);return a;}template<typename T>T mul_inv(T a,T b){T t1=a,t2=b,t3;T v1=1,v2=0,v3;T x;while(t2!=1)x=t1/t2,t3=t1-x*t2,v3=v1-x*v2,t1=t2,t2=t3,v1=v2,v2=v3;return(v2+b)%b;}template<typename T>T powmod(T a,T b,T MOD){if(b<0)return 0;T rv=1;while(b)(b%2)&&(rv=(rv*a)%MOD),a=a*a%MOD,b/=2;return rv;}template<typename T>inline T isqrt(T k){T r=sqrt((double)k)+1;while(r*r>k)r--;return r;}template<typename T>inline T icbrt(T k){T r=cbrt((double)k)+1;while(r*r*r>k)r--;return r;}template<typename T>bool mul_overflow(T&r,T a,T b){return __builtin_mul_overflow(a,b,&r);}template<ll n>struct BitSize{enum{Size=BitSize<n/2>::Size+1};};template<>struct BitSize<0>{enum{Size=1};};template<>struct BitSize<1>{enum{Size=1};};
#define BITSIZE(n) (BitSize<n>::Size)
#define TREESIZE(n) ((1<<(BitSize<n>::Size + 1))+7)
#define BITMAX(n) (BitSize<n>::Size - 1)
#define DEBUG !defined(ONLINE_JUDGE)
#define SZ(x) ((int)(x).size())
#define ALL(x) begin(x), end(x)
#define RALL(x) (x).rbegin(),(x).rend()
template<typename TH>void _dbg(const char*sdbg,TH h){cerr<<sdbg<<"="<<h<<"\n";}template<typename TH,typename... TA>void _dbg(const char*sdbg,TH h,TA... t){while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<",";_dbg(sdbg+1,t...);}
#ifdef DEBUG
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; for(auto itt: x) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif
}namespace vh{template<typename T>int min_rotation(vector<T>s){int a=0,N=s.size();for(int b=0;b<N;b++)for(int i=0;i<N;i++){if(a+i==b||s[(a+i)%N]<s[(b+i)%N]){b+=max(0,i-1);break;}if(s[(a+i)%N]>s[(b+i)%N]){a=b;break;}}return a;};template<typename T>class StackGuard{private:T x;public:StackGuard(T x):x(x){}~StackGuard(){x();}};}namespace vh{};
namespace vh{
  const int maxn=200200;
  int n,q;
  int a[maxn];
  int mi[maxn],ma[maxn];
  int cnt[maxn];
  int main(){
    srand(GRANDOM);
    cin>>n>>q;
    memset(mi,-1,sizeof(mi));
    memset(ma,-1,sizeof(ma));
    memset(cnt,0,sizeof(cnt));
    set<int>v;
    for(int i=0;i<n;i++){
      cin>>a[i];
      v.insert(a[i]);
      cnt[a[i]]++;
      if(mi[a[i]]<0)mi[a[i]]=ma[a[i]]=i;
      else ma[a[i]]=i;
    }
    int cs=0,cmx=0,ce=-1;
    int ans=0;
    auto _commit=[&](){
      if(ce>=cs){
        ans+=(ce-cs+1)-cmx;
      }
    };
    for(int i=0;i<n;i++){
      if(i>ce){
        _commit();
        cs=i;cmx=cnt[a[i]];ce=ma[a[i]];
      }else{
        cmx=max(cmx,cnt[a[i]]);
        ce=max(ce,ma[a[i]]);
      }
    }
    _commit();
    cout<<ans<<endl;
    return 0;
  }
};
int main(int argc,char*argv[]){
  std::cin.sync_with_stdio(false);std::cin.tie(nullptr);
  return vh::main();
}


#pragma region [Header Files]
#include <iostream>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <stdlib.h>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <bitset>

using namespace std;

typedef long long LL;
typedef unsigned long long uLL;
typedef vector<int> vi;
typedef vector<LL> VLL;
typedef pair<int, int> pii;

#define pi acos(-1)
#define pre setprecision(10)
#define pb push_back
#define sz(a) ((long)a.size())
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define sqr(a) ((a) * (a))
#define fr(i, n) for (i = 1; i <= n; i++)
#define fr0(i, n) for (i = 0; i < n; i++)
#define faster_io() ios_base::sync_with_stdio(false)
#define zero(a) memset(a, 0, sizeof(a))
#define in(a) scanf("%d", &a)
#pragma endregion
#define mx 200005

int a[mx], c[mx], r[mx];

int main()
{
 //   freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    faster_io();

    int q,n,x,i,rX,cnt,mX,ans;

    zero(c);

    cin >> n >> q;
    fr(i,n){
        cin >> x;
        a[i]=x, c[x]++, r[x]=i;
    }

    i=1, ans=0;
    while(i<=n){
        rX = r[a[i]];
        cnt=0, mX=0;
        while(i<rX){
            //printf("%d %d %d \n",i,a[i],rX);
            cnt++, mX = max(mX, c[a[i]]);
            i++;
            rX = max(rX, r[a[i]]);
        }
        if(c[a[i]]>1)   cnt++;
        i++;
        //printf("%d %d \n", cnt, mX);
        ans += (cnt-mX);
    }

    cout << ans << endl;

    return 0;
}


#pragma region [Header Files]
#include <iostream>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <stdlib.h>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <bitset>

using namespace std;

typedef long long LL;
typedef unsigned long long uLL;
typedef vector<int> vi;
typedef vector<LL> VLL;
typedef pair<int, int> pii;

#define pi acos(-1)
#define pre setprecision(10)
#define pb push_back
#define sz(a) ((long)a.size())
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define sqr(a) ((a) * (a))
#define fr(i, n) for (i = 1; i <= n; i++)
#define fr0(i, n) for (i = 0; i < n; i++)
#define faster_io() ios_base::sync_with_stdio(false)
#define zero(a) memset(a, 0, sizeof(a))
#define in(a) scanf("%d", &a)
#pragma endregion
#define mx 200005

int a[mx], c[mx], r[mx];

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    faster_io();

    int q,n,x,i,rX,cnt,mX,ans;

    zero(c);

    cin >> n >> q;
    fr(i,n){
        cin >> x;
        a[i]=x, c[x]++, r[x]=i;
    }

    i=1, ans=0;
    while(i<=n){
        rX = r[a[i]];
        cnt=0, mX=0;
        while(i<rX){
            //printf("%d %d %d \n",i,a[i],rX);
            cnt++, mX = max(mX, c[a[i]]);
            i++;
            rX = max(rX, r[a[i]]);
        }
        i++;
        if(c[a[i]]>1)   cnt++;
        //printf("%d %d \n", cnt, mX);
        ans += (cnt-mX);
    }

    cout << ans << endl;

    return 0;
}


#pragma region [Header Files]
#include <iostream>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <stdlib.h>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <bitset>

using namespace std;

typedef long long LL;
typedef unsigned long long uLL;
typedef vector<int> vi;
typedef vector<LL> VLL;
typedef pair<int, int> pii;

#define pi acos(-1)
#define pre setprecision(10)
#define pb push_back
#define sz(a) ((long)a.size())
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define sqr(a) ((a) * (a))
#define fr(i, n) for (i = 1; i <= n; i++)
#define fr0(i, n) for (i = 0; i < n; i++)
#define faster_io() ios_base::sync_with_stdio(false)
#define zero(a) memset(a, 0, sizeof(a))
#define in(a) scanf("%d", &a)
#pragma endregion
#define mx 200005

int a[mx];
bool b[mx];

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    faster_io();

    int q,n,x,y,mxA,sA;

    zero(a), zero(b), sA=0, mxA=0, y=0;
    cin >> n >> q;
    while(n--){
        cin >> x;
        a[x]++;
        if(x!=y){
            if(a[x]>1){
                if(!b[x]){
                    sA += a[x];
                    mxA = max(mxA, a[x]);
                    b[x] = 1;
                    //cout << "1 : ";
                }
                else if(b[x]){
                    sA++;
                    mxA = max(mxA, a[x]);
                    //cout << "2 : ";
                }
                if(!b[y]){
                    sA++;
                    mxA = max(mxA, 1);
                    b[y] = 1;
                    //cout << "3 : ";
                }
            }
        }
        else if(x==y && b[x]){
            sA++;
            mxA = max(mxA, a[x]);
        }
        //cout << sA << " " << mxA << " " << x << endl;
        y=x;
    }

    cout << sA-mxA << endl;

    return 0;
}


#pragma region [Header Files]
#include <iostream>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <stdlib.h>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <bitset>

using namespace std;

typedef long long LL;
typedef unsigned long long uLL;
typedef vector<int> vi;
typedef vector<LL> VLL;
typedef pair<int, int> pii;

#define pi acos(-1)
#define pre setprecision(10)
#define pb push_back
#define sz(a) ((long)a.size())
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define sqr(a) ((a) * (a))
#define fr(i, n) for (i = 1; i <= n; i++)
#define fr0(i, n) for (i = 0; i < n; i++)
#define faster_io() ios_base::sync_with_stdio(false)
#define zero(a) memset(a, 0, sizeof(a))
#define in(a) scanf("%d", &a)
#pragma endregion
#define mx 200005

int a[mx], c[mx], r[mx];

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    faster_io();

    int q,n,x,i,rX,cnt,mX,ans;

    zero(c);

    cin >> n >> q;
    fr(i,n){
        cin >> x;
        a[i]=x, c[x]++, r[x]=i;
    }

    i=1, ans=0;
    while(i<=n){
        rX = r[a[i]];
        cnt=0, mX=0;
        while(i<rX){
            //printf("%d %d %d \n",i,a[i],rX);
            cnt++, mX = max(mX, c[a[i]]);
            i++;
            rX = max(rX, r[a[i]]);
        }
        i++, cnt++;
        //printf("%d %d \n", cnt, mX);
        ans += (cnt-mX);
    }

    cout << ans << endl;

    return 0;
}


#include <bits/stdc++.h>
#define maxn 200005
const int lim = 2e5;

int n,q,a[maxn],cnt[maxn],x,y;
std::set<int>S[maxn];

struct data {
	int l,r,min,ans;
};

struct mikeMirzayanov {
	int max[maxn<<2];
	void modify(int l,int r,int p,int v,int rt){
		if(l==r){max[rt]=v;return;}
		int mid=(l+r)>>1;if(p<=mid)modify(l,mid,p,v,rt<<1);else modify(mid+1,r,p,v,rt<<1|1);
		max[rt]=std::max(max[rt<<1],max[rt<<1|1]);
	} int query(int l,int r,int L,int R,int rt){
		if(l>R||r<L)return 0;if(l<=L&&R<=r)return max[rt];return std::max(query(l,r,L,(L+R)>>1,rt<<1),query(l,r,((L+R)>>1)+1,R,rt<<1|1));
	}
}W;

data merge(data a,data b) {
	if (a.min > b.min) std::swap(a,b);
	if (a.min != b.min) return a;
	else return data({a.l,b.r,a.min,W.query(a.l+1,b.r,1,n,1) + a.ans + b.ans});
}

struct segmentTree {
	data sum[maxn<<2];
	int tag[maxn<<2];
	void update(int x) { sum[x] = merge(sum[x<<1],sum[x<<1|1]); }

	void give_tag(int x,int y) {
		sum[x].min += y;
		tag[x] += y;
	}

	void pushdown(int x) {
		if (!tag[x]) return;
		give_tag(x<<1,tag[x]);
		give_tag(x<<1|1,tag[x]);
		tag[x] = 0;
	}

	void build(int l,int r,int rt) {
		if (l == r) { sum[rt] = data({l,l,0,0}); return; }
		build(l,(l+r)>>1,rt<<1);
		build(((l+r)>>1)+1,r,rt<<1|1);
		update(rt);

	}

	void add(int l,int r,int L,int R,int v,int rt) {
		if (l>R||r<L)return;
		if(l<=L&&R<=r) { give_tag(rt,v); return; }
		pushdown(rt);
		add(l,r,L,(L+R)>>1,v,rt<<1);
		add(l,r,((L+R)>>1)+1,R,v,rt<<1|1);
		update(rt);
	}
	void dfs(int l,int r,int rt){

		if(l==r)return;
		pushdown(rt);
		dfs(l,(l+r)>>1,rt<<1);
		dfs(((l+r)>>1)+1,r,rt<<1|1);
		update(rt);
	//	printf("[%d %d] %d %d %d <%d>\n",l,r,sum[rt].l,sum[rt].r,sum[rt].ans,sum[rt].min);
	}
	void visit(int l,int r,int L,int R,int rt){
		if(l>R||r<L)return;
		if(l<=L&&R<=r) return;
		pushdown(rt);
		visit(l,r,L,(L+R)>>1,rt<<1);
		visit(l,r,((L+R)>>1)+1,R,rt<<1|1);
		update(rt);
	}
}A;

void inc(int x) {
	if (S[x].empty()) return;
	W.modify(1,n,*S[x].begin(),S[x].size(),1);
	if (S[x].size() > 1) A.add(*S[x].begin(),*S[x].rbegin()-1,0,n,1,1);
}

void dec(int x) {
	if (S[x].empty()) return;
	W.modify(1,n,*S[x].begin(),0,1);
	if (S[x].size() > 1) A.add(*S[x].begin(),*S[x].rbegin()-1,0,n,-1,1);
}

int main() {
	scanf("%d%d",&n,&q);
	for (int i = 1; i <= n; ++ i) {
		scanf("%d",&a[i]);
		S[a[i]].insert(i);
	}A.build(0,n,1);
	for (int i = 1; i <= lim; ++ i) inc(i);

	A.dfs(0,n,1);
	printf("%d\n",n-A.sum[1].ans);
	while (q--) {
		scanf("%d%d",&x,&y);
		dec(a[x]); dec(y);
		int last = a[x];
		S[a[x]].erase(x); a[x] = y; S[y].insert(x);
		inc(last); inc(y);
		A.visit(*S[y].begin(),*S[y].rbegin(),1,n,1);
		//A.dfs(0,n,1);
		if (S[last].size()) A.visit(*S[last].begin(),*S[last].rbegin(),0,n,1);
	//	for (int i = 1; i <= n; ++ i) printf("%d ",W.query(i,i,1,n,1)); puts("");
		printf("%d\n",n-A.sum[1].ans);
	}
	return 0;
}


#include <bits/stdc++.h>
#define maxn 200005

int n,q,a[maxn],L[maxn],R[maxn],cnt[maxn];

int solve(int l,int r) {
	if (l > r) return 0;
	int lm = 0, mx = 0;
	for (int i = l; i <= r; ++ i) {
		lm = std::max(lm,R[a[i]]);
		mx = std::max(mx,cnt[a[i]]);
		if (lm == i)
			return mx + solve(i+1,r);
	} return 0;
}

int main() {
	scanf("%d%d",&n,&q);
	for (int i = 1; i <= n; ++ i) {
		scanf("%d",&a[i]);
		cnt[a[i]] ++;
		//if (!l[a[i]]) l[a[i]] = i;
		R[a[i]] = i;
	} printf("%d",n-solve(1,n));
	return 0;
}


#include <bits/stdc++.h>
#define maxn 100005

int n,q,a[maxn],L[maxn],R[maxn],cnt[maxn];

int solve(int l,int r) {
	if (l > r) return 0;
	int lm = 0, mx = 0;
	for (int i = l; i <= r; ++ i) {
		lm = std::max(lm,R[a[i]]);
		mx = std::max(mx,cnt[a[i]]);
		if (lm == i)
			return mx + solve(i+1,r);
	} return 0;
}

int main() {
	scanf("%d%d",&n,&q);
	for (int i = 1; i <= n; ++ i) {
		scanf("%d",&a[i]);
		cnt[a[i]] ++;
		//if (!l[a[i]]) l[a[i]] = i;
		R[a[i]] = i;
	} printf("%d",n-solve(1,n));
	return 0;
}


#include <iostream>
#include <utility>
#include <map>
#include <vector>
#include <algorithm>
#define pii pair<int,int>
#define F first
#define S second
#define ericxiao ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;

const int maxC = 2e5 + 10;
bool has[maxC];
int N, q, numcnt = 0, frq[maxC], res;
vector<int> vec;
vector<pii> times;
map<int, pii> pos;

inline int getmode(int l, int r){
    vector<int> toundo = vector<int>();
    res = 0;
    for(int i = l; i <= r; i++){
        frq[vec[i]]++;
        res = max(res, frq[vec[i]]);
        toundo.push_back(vec[i]);
    }
    for(int i : toundo) frq[i]--;
    return res;
}

int main(){
    ericxiao;
    fill(frq, frq + maxC, 0);
    cin >> N >> q; //q = 0
    vec.resize(N);
    for(int i = 0; i < N; i++){
        cin >> vec[i];
        if(!pos.count(vec[i])) pos[vec[i]] = {i, i};
        else pos[vec[i]].S = i;
    }
    for(auto p : pos){
        times.push_back({p.S.F, p.F});
        times.push_back({p.S.S, p.F});
        //cout << p.F << " START: " << p.S.F << ", END: " << p.S.S << endl;
    }
    sort(times.begin(), times.end());
    fill(has, has + maxC, false);
    int ans = 0;
    int cl = 0, cr;
    for(pii p : times){
        //cout << "time = " << p.F << ", num = " << p.S << endl;
        if(!has[p.S]){
            has[p.S] = true;
            numcnt++;
        } else {
            has[p.S] = false;
            numcnt--;
        }
        if(!numcnt){
            //cout << "HEY" << endl;
            cr = p.F;
            ans += (cr - cl + 1) - getmode(cl, cr);
            cl = cr + 1;
        }
    }
    cout << ans << endl;
}


#include <iostream>
#include <utility>
#include <map>
#include <vector>
#include <algorithm>
#define pii pair<int,int>
#define F first
#define S second
using namespace std;

const int maxC = 2e5 + 10;
int N, q, numcnt = 0;
vector<int> vec;
vector<pii> times;
map<int, pii> pos;

inline int getmode(int l, int r){
    static int frq[maxC], res;
    fill(frq, frq + maxC, 0);
    res = 0;
    for(int i = l; i <= r; i++){
        frq[vec[i]]++;
        res = max(res, frq[vec[i]]);
    }
    return res;
}

int main(){
    cin >> N >> q; //q = 0
    vec.resize(N);
    for(int i = 0; i < N; i++){
        cin >> vec[i];
        if(!pos.count(vec[i])) pos[vec[i]] = {i, i};
        else pos[vec[i]].S = i;
    }
    for(auto p : pos){
        times.push_back({p.S.F, p.F});
        times.push_back({p.S.S, p.F});
        //cout << p.F << " START: " << p.S.F << ", END: " << p.S.S << endl;
    }
    sort(times.begin(), times.end());
    int t = 0;
    int ans = 0;
    int cl = 0, cr;
    for(pii p : times){
        t ^= p.S;
        if(!t){
            cr = p.F;
            ans += (cr - cl + 1) - getmode(cl, cr);
            cl = cr + 1;
        }
    }
    cout << ans << endl;
}


#include <iostream>
#include <utility>
#include <map>
#include <vector>
#include <algorithm>
#define pii pair<int,int>
#define F first
#define S second
#define ericxiao ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;

const int maxC = 2e5 + 10;
bool has[maxC];
int N, q, numcnt = 0;
vector<int> vec;
vector<pii> times;
map<int, pii> pos;

inline int getmode(int l, int r){
    static int frq[maxC], res;
    fill(frq, frq + maxC, 0);
    res = 0;
    for(int i = l; i <= r; i++){
        frq[vec[i]]++;
        res = max(res, frq[vec[i]]);
    }
    return res;
}

int main(){
    ericxiao;
    cin >> N >> q; //q = 0
    vec.resize(N);
    for(int i = 0; i < N; i++){
        cin >> vec[i];
        if(!pos.count(vec[i])) pos[vec[i]] = {i, i};
        else pos[vec[i]].S = i;
    }
    for(auto p : pos){
        times.push_back({p.S.F, p.F});
        times.push_back({p.S.S, p.F});
        //cout << p.F << " START: " << p.S.F << ", END: " << p.S.S << endl;
    }
    sort(times.begin(), times.end());
    fill(has, has + maxC, false);
    int ans = 0;
    int cl = 0, cr;
    for(pii p : times){
        //cout << "time = " << p.F << ", num = " << p.S << endl;
        if(!has[p.S]){
            has[p.S] = true;
            numcnt++;
        } else {
            has[p.S] = false;
            numcnt--;
        }
        if(!numcnt){
            //cout << "HEY" << endl;
            cr = p.F;
            ans += (cr - cl + 1) - getmode(cl, cr);
            cl = cr + 1;
        }
    }
    cout << ans << endl;
}


#include <iostream>
#include <utility>
#include <map>
#include <vector>
#include <algorithm>
#define pii pair<int,int>
#define F first
#define S second
#define ericxiao ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;

const int maxC = 2e5 + 10;
bool has[maxC], frq[maxC], res;
int N, q, numcnt = 0;
vector<int> vec;
vector<pii> times;
map<int, pii> pos;

inline int getmode(int l, int r){
    vector<int> toundo = vector<int>();
    res = 0;
    for(int i = l; i <= r; i++){
        frq[vec[i]]++;
        res = max(res, frq[vec[i]]);
        toundo.push_back(vec[i]);
    }
    for(int i : toundo) frq[i]--;
    return res;
}

int main(){
    ericxiao;
    fill(frq, frq + maxC, 0);
    cin >> N >> q; //q = 0
    vec.resize(N);
    for(int i = 0; i < N; i++){
        cin >> vec[i];
        if(!pos.count(vec[i])) pos[vec[i]] = {i, i};
        else pos[vec[i]].S = i;
    }
    for(auto p : pos){
        times.push_back({p.S.F, p.F});
        times.push_back({p.S.S, p.F});
        //cout << p.F << " START: " << p.S.F << ", END: " << p.S.S << endl;
    }
    sort(times.begin(), times.end());
    fill(has, has + maxC, false);
    int ans = 0;
    int cl = 0, cr;
    for(pii p : times){
        //cout << "time = " << p.F << ", num = " << p.S << endl;
        if(!has[p.S]){
            has[p.S] = true;
            numcnt++;
        } else {
            has[p.S] = false;
            numcnt--;
        }
        if(!numcnt){
            //cout << "HEY" << endl;
            cr = p.F;
            ans += (cr - cl + 1) - getmode(cl, cr);
            cl = cr + 1;
        }
    }
    cout << ans << endl;
}


#include <iostream>
#include <utility>
#include <map>
#include <vector>
#include <algorithm>
#define pii pair<int,int>
#define F first
#define S second
using namespace std;

const int maxC = 2e5 + 10;
bool has[maxC];
int N, q, numcnt = 0;
vector<int> vec;
vector<pii> times;
map<int, pii> pos;

inline int getmode(int l, int r){
    static int frq[maxC], res;
    fill(frq, frq + maxC, 0);
    res = 0;
    for(int i = l; i <= r; i++){
        frq[vec[i]]++;
        res = max(res, frq[vec[i]]);
    }
    return res;
}

int main(){
    cin >> N >> q; //q = 0
    vec.resize(N);
    for(int i = 0; i < N; i++){
        cin >> vec[i];
        if(!pos.count(vec[i])) pos[vec[i]] = {i, i};
        else pos[vec[i]].S = i;
    }
    for(auto p : pos){
        times.push_back({p.S.F, p.F});
        times.push_back({p.S.S, p.F});
    }
    sort(times.begin(), times.end());
    fill(has, has + maxC, false);
    numcnt = 0;
    int ans = 0;
    int cl = 0, cr;
    for(pii p : times){
        if(!has[p.S]){
            has[p.S] = true;
            cl++;
        } else {
            has[p.S] = false;
            cl--;
        }
        if(!cl){
            cr = p.F;
            ans += (cr - cl + 1) - getmode(cl, cr);
            cl = cr + 1;
        }
    }
    cout << ans << endl;
}


#include <iostream>
#include <utility>
#include <map>
#include <vector>
#include <algorithm>
#define pii pair<int,int>
#define F first
#define S second
using namespace std;

const int maxC = 2e5 + 10;
bool has[maxC];
int N, q, numcnt = 0;
vector<int> vec;
vector<pii> times;
map<int, pii> pos;

inline int getmode(int l, int r){
    static int frq[maxC], res;
    fill(frq, frq + maxC, 0);
    res = 0;
    for(int i = l; i <= r; i++){
        frq[vec[i]]++;
        res = max(res, frq[vec[i]]);
    }
    return res;
}

int main(){
    cin >> N >> q; //q = 0
    vec.resize(N);
    for(int i = 0; i < N; i++){
        cin >> vec[i];
        if(!pos.count(vec[i])) pos[vec[i]] = {i, i};
        else pos[vec[i]].S = i;
    }
    for(auto p : pos){
        times.push_back({p.S.F, p.F});
        times.push_back({p.S.S, p.F});
        //cout << p.F << " START: " << p.S.F << ", END: " << p.S.S << endl;
    }
    sort(times.begin(), times.end());
    fill(has, has + maxC, false);
    int ans = 0;
    int cl = 0, cr;
    for(pii p : times){
        //cout << "time = " << p.F << ", num = " << p.S << endl;
        if(!has[p.S]){
            has[p.S] = true;
            numcnt++;
        } else {
            has[p.S] = false;
            numcnt--;
        }
        if(!numcnt){
            //cout << "HEY" << endl;
            cr = p.F;
            ans += (cr - cl + 1) - getmode(cl, cr);
            cl = cr + 1;
        }
    }
    cout << ans << endl;
}


#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_WARINGS
#define _USE_MATH_DEFINES

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <memory.h>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <stack>
#include <utility>
#include <algorithm>
#include <functional>
#include <random>
#include <bitset>
#include <limits>

using namespace std;

typedef long long int64;
typedef unsigned long long uint64;
typedef pair<int, int> pii;
typedef pair<int64, int64> pll;

const int INF = (int)(1e9 + 1337);
const int64 LINF = (int64)(4e18);
const double EPS = (double)(1e-7);
#define sq(x) ((x)*(x))
#define FAIL() ((*(int*)(0))++)

typedef long long int ll;

int n, q;
vector<int> a;
unordered_map<int, int> mx;
unordered_map<int, int> mn;
unordered_map<int, int> ct;
vector<vector<int> > g;
vector<int> c;

void dfs(int v, int nc, int& maj, int& total) {
    c[v] = nc;
    maj = max(maj, ct[v]);
    total += ct[v];
    for (int i = 0; i < g[v].size(); ++i) {
        int to = g[v][i];
        if (c[to] != -1) {
            continue;
        }
        dfs(to, nc, maj, total);
    }
}

void solve()
{
    cin >> n >> q;
    a.resize(n);
    int gmx = 0;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        gmx = max(gmx, a[i]);
        mx[a[i]] = i;
        if (!mn.count(a[i])) {
            mn[a[i]] = i;
        }
        ++ct[a[i]];
    }
    g.resize(gmx + 1);
    c.assign(gmx + 1, -1);
    vector<int> st;
    for (int i = 0; i + 1 < n; ++i) {
        if (mn[a[i]] == i) {
            st.push_back(a[i]);
        }
        if (mx[a[i]] == i) {
            st.pop_back();
        }
        if (!st.empty()) {
            g[a[i]].push_back(st.back());
            g[st.back()].push_back(a[i]);
        }
    }
    int ans = 0;
    for (const auto& it : mx) {
        int x = it.first;
        if (c[x] != -1) {
            continue;
        }
        int maj = 0;
        int total = 0;
        dfs(x, x, maj, total);
        ans += total - maj;
    }
    cout << ans;
}

int main() {
#ifdef _MY_DEBUG
    freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#endif

    ios_base::sync_with_stdio(false); cin.tie(0);

    solve();

    return 0;
}


#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_WARINGS
#define _USE_MATH_DEFINES

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <memory.h>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <stack>
#include <utility>
#include <algorithm>
#include <functional>
#include <random>
#include <bitset>
#include <limits>

using namespace std;

typedef long long int64;
typedef unsigned long long uint64;
typedef pair<int, int> pii;
typedef pair<int64, int64> pll;

const int INF = (int)(1e9 + 1337);
const int64 LINF = (int64)(4e18);
const double EPS = (double)(1e-7);
#define sq(x) ((x)*(x))
#define FAIL() ((*(int*)(0))++)

typedef long long int ll;

int n, q;
vector<int> a;
unordered_map<int, int> mx;
unordered_map<int, int> mn;
unordered_map<int, int> ct;
vector<vector<int> > g;
vector<int> c;

void dfs(int v, int nc, int& maj, int& total) {
    c[v] = nc;
    maj = max(maj, ct[v]);
    total += ct[v];
    for (int i = 0; i < g[v].size(); ++i) {
        int to = g[v][i];
        if (c[to] != -1) {
            continue;
        }
        dfs(to, nc, maj, total);
    }
}

void solve()
{
    cin >> n >> q;
    a.resize(n);
    int gmx = 0;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        gmx = max(gmx, a[i]);
        mx[a[i]] = i;
        if (!mn.count(a[i])) {
            mn[a[i]] = i;
        }
        ++ct[a[i]];
    }
    g.resize(gmx + 1);
    c.assign(gmx + 1, -1);
    unordered_set<int> st;
    for (int i = 0; i + 1 < n; ++i) {
        if (mn[a[i]] == i) {
            st.insert(a[i]);
        }
        if (mx[a[i]] == i) {
            st.erase(a[i]);
        }
        if (!st.empty()) {
            g[a[i]].push_back(*st.begin());
            g[*st.begin()].push_back(a[i]);
        }
    }
    int ans = 0;
    for (const auto& it : mx) {
        int x = it.first;
        if (c[x] != -1) {
            continue;
        }
        int maj = 0;
        int total = 0;
        dfs(x, x, maj, total);
        ans += total - maj;
    }
    cout << ans;
}

int main() {
#ifdef _MY_DEBUG
    freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#endif

    ios_base::sync_with_stdio(false); cin.tie(0);

    solve();

    return 0;
}


#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_WARINGS
#define _USE_MATH_DEFINES

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <memory.h>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <stack>
#include <utility>
#include <algorithm>
#include <functional>
#include <random>
#include <bitset>
#include <limits>

using namespace std;

typedef long long int64;
typedef unsigned long long uint64;
typedef pair<int, int> pii;
typedef pair<int64, int64> pll;

const int INF = (int)(1e9 + 1337);
const int64 LINF = (int64)(4e18);
const double EPS = (double)(1e-7);
#define sq(x) ((x)*(x))
#define FAIL() ((*(int*)(0))++)

typedef long long int ll;

int n, q;
vector<int> a;
unordered_map<int, int> mx;
unordered_map<int, int> mn;
unordered_map<int, int> ct;
vector<vector<int> > g;
vector<int> c;

void dfs(int v, int nc, int& maj, int& total) {
    c[v] = nc;
    maj = max(maj, ct[v]);
    total += ct[v];
    for (int i = 0; i < g[v].size(); ++i) {
        int to = g[v][i];
        if (c[to] != -1) {
            continue;
        }
        dfs(to, nc, maj, total);
    }
}

void solve()
{
    cin >> n >> q;
    a.resize(n);
    int gmx = 0;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        gmx = max(gmx, a[i]);
        mx[a[i]] = i;
        if (!mn.count(a[i])) {
            mn[a[i]] = i;
        }
        ++ct[a[i]];
    }
    g.resize(gmx + 1);
    c.assign(gmx + 1, -1);
    for (int i = 0; i + 1 < n; ++i) {
        if (mx[a[i]] != i) {
            g[a[i]].push_back(a[i + 1]);
            g[a[i + 1]].push_back(a[i]);
        }
        if (mn[a[i]] != i) {
            g[a[i]].push_back(a[i - 1]);
            g[a[i - 1]].push_back(a[i]);
        }
    }
    int ans = 0;
    for (const auto& it : mx) {
        int x = it.first;
        if (c[x] != -1) {
            continue;
        }
        int maj = 0;
        int total = 0;
        dfs(x, x, maj, total);
        ans += total - maj;
    }
    cout << ans;
}

int main() {
#ifdef _MY_DEBUG
    freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#endif

    ios_base::sync_with_stdio(false); cin.tie(0);

    solve();

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn=2e5+10;
int a[maxn],vis[maxn];
int ans[maxn],n,q;
int main()
{
#ifdef LOCAL
    freopen("input.txt","r",stdin);
#endif
    scanf("%d%d",&n,&q);int res=n;
    for(int i=1;i<=n;i++)scanf("%d",&a[i]),vis[a[i]]=i;
    for(int i=1;i<=n;)
    {
        int tmp=vis[a[i]],maxx=0;
        for (int j = i; j <=tmp; ++j)
        {
            ans[a[j]]++;maxx=max(maxx,ans[a[j]]);
            tmp=max(tmp,vis[a[j]]);
        }
        for (int j = i; j <= tmp; ++j)
            ans[a[j]]--;
        res -= maxx ;i = tmp + 1;
    }
    printf("%d\n",res);
    return 0;
}


//  Author wzazzy ^_^
//  keep a good mental state and stick to it
//  The devil is in the details
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn=2e5+10;
int a[maxn];
int c[maxn]; //每个位置往右扩到最远的位置
int vis[maxn];//每种数字出现的最右边的位置
int cnt[maxn];//每种数字出现的次数
int ans,n,q;
int main()
{
#ifdef LOCAL
    freopen("input.txt","r",stdin);
#endif
    scanf("%d%d",&n,&q);int ans=n;
    for(int i=1;i<=n;i++)scanf("%d",&a[i]),vis[a[i]]=i,cnt[a[i]]++;
    for(int i=1;i<=n;i++)c[i]=max(c[i-1],vis[a[i]]);
    int ls=1;
    for(int i=1;i<=n;i++)
    	if(i==c[i])
    	{
    		int mx=0;
    		for(int j=ls;j<=i;j++)mx=max(mx,cnt[a[j]]);
    		ans-=mx;
    	    ls=i+1;
    	}
    printf("%d\n",ans);
    return 0;
}


//  Author wzazzy ^_^
//  keep a good mental state and stick to it
//  The devil is in the details
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn=2e5+10;
int a[maxn];
int c[maxn]; //每个位置往右扩到最远的位置
int vis[maxn];//每种数字出现的最右边的位置
int cnt[maxn];//每种数字出现的次数
int ans,n,q;
int main()
{
#ifdef LOCAL
    freopen("input.txt","r",stdin);
#endif
    scanf("%d%d",&n,&q);int ans=n;
    for(int i=1;i<=n;i++)scanf("%d",&a[i]),vis[a[i]]=i;
    for(int i=1;i<=n;)
    {
    	int tmp=vis[a[i]],maxx=0;
    	for(int j=i;j<=tmp;j++)
    	{
    		cnt[a[j]]++;maxx=max(maxx,cnt[a[j]]);
    		tmp=max(tmp,vis[a[j]]);
    	}
    	for(int j=i;j<=tmp;j++)cnt[a[j]]--;
    	ans-=maxx;i=tmp+1;
    }
    printf("%d\n",ans);
    return 0;
}


#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define all(a) a.begin(), a.end()
#define ff(i,a,b) for(int i=a;i<=b;i++)
#define fb(i,b,a) for(int i=b;i>=a;i--)

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 200005;
const int inf = 1e9 + 5;

int n, q;
int niz[maxn];

int kec[maxn];
int dva[maxn];
int desno[maxn];
int cnt[maxn];

bool ima[maxn];
bool bio[maxn];

vector<pii> vec;
vector<pii> sta;

int main()
{
    ios::sync_with_stdio(false);
    cout.tie(nullptr);
    cin.tie(nullptr);
    cin >> n >> q;
    ff(i,1,n)cin >> niz[i];
    ff(i,1,n){
        int x = niz[i];
        if(!kec[x])kec[x] = i;
        dva[x] = i;
    }
    ff(i,1,maxn - 1){
        if(!kec[i])continue;
        desno[kec[i]] = dva[i];
        vec.pb({kec[i], dva[i]});
        ima[kec[i]] = 1;
    }
//    cout << endl;
    sort(all(vec));
    for(auto c : vec){
        int l = c.fi;
        int r = c.se;
        if(bio[l])continue;
        ff(i,l,r){
            bio[i] = 1;
            if(ima[i])r = max(r, desno[i]);
        }
        sta.pb({l, r});
    }
    int rez = 0;
    for(auto c : sta){
        int l = c.fi;
        int r = c.se;
//        cout << l << " " << r << endl;
        int najv = 0;
        ff(i,l,r)najv = max(najv, ++cnt[niz[i]]);
        rez += r - l + 1 - najv;
    }
    cout << rez << endl;
    return 0;
}
/*

*/


#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 200005;
int n, q;
int niz[maxn];
int prvi[maxn];
int last[maxn];
int ima[maxn];
int desno[maxn];
int cnt[maxn];
bool bio[maxn];
vector<pii> v, sta;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cin >> n >> q;
    for(int i=1;i<=n;i++){
        cin >> niz[i];
        if(!prvi[niz[i]])prvi[niz[i]] = i;
        last[niz[i]] = i;
    }
    for(int i=1;i<=maxn;i++){
        if(!prvi[i])continue;
        desno[prvi[i]] = last[i];
        v.pb({prvi[i], last[i]});
        ima[prvi[i]] = 1;
    }
    int len = v.size();
    sort(v.begin(), v.end());
    for(auto c : v){
        int l = c.fi;
        int r = c.se;
        if(bio[l])continue;
        bio[l] = 1;
        for(int j=l;j<=r;j++){
            bio[j] = 1;
            if(ima[j])r = max(r, desno[j]);
        }
        sta.pb({l, r});
    }
    int rez = 0;
    for(auto c : sta){
        int l = c.fi;
        int r = c.se;
        int najv = 0;
        for(int j=l;j<=r;j++)najv = max(najv, ++cnt[niz[j]]);
        rez += r - l + 1 - najv;
    }
    cout << rez << endl;
    return 0;
}
/*
6 0
3 1 3 1 2 2
*/


#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 200005;
int n, q;
int niz[maxn];
int prvi[maxn];
int last[maxn];
int ima[maxn];
int desno[maxn];
int cnt[maxn];
bool bio[maxn];
vector<pii> v, sta;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cin >> n >> q;
    for(int i=1;i<=n;i++){
        cin >> niz[i];
        if(!prvi[niz[i]])prvi[niz[i]] = i;
        last[niz[i]] = i;
    }
    for(int i=1;i<maxn;i++){
        if(!prvi[i])continue;
        desno[prvi[i]] = last[i];
        v.pb({prvi[i], last[i]});
        ima[prvi[i]] = 1;
    }
    int len = v.size();
    sort(v.begin(), v.end());
    for(auto c : v){
        int l = c.fi;
        int r = c.se;
        if(bio[l])continue;
        bio[l] = 1;
        for(int j=l;j<=r;j++){
            bio[j] = 1;
            if(ima[j])r = max(r, desno[j]);
        }
        sta.pb({l, r});
    }
    int rez = 0;
    for(auto c : sta){
        int l = c.fi;
        int r = c.se;
        int najv = 0;
        for(int j=l;j<=r;j++)najv = max(najv, ++cnt[niz[j]]);
        rez += r - l + 1 - najv;
    }
    cout << rez << endl;
    return 0;
}
/*
6 0
3 1 3 1 2 2
*/


/*
 * 214B_Hometask.cpp
 *
 *  Created on: Aug 31, 2014
 *      Author: Ahmed
 */

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

int main() {
    //#ifndef ONLINE_JUDGE
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    //#endif
    int n;
    char c;
    int r1 = 0, r2 = 0;
    string res = "";
    vector<char> input;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> c;
        input.push_back(c);
        if ((c - '0') % 3 == 1) r1++;
        else if ((c - '0') % 3 == 2) r2++;
    }

    if (r1 > 0) r1 %= 3;
    if (r2 > 0) r2 %= 3;

    sort(input.begin(), input.end());

    if (input[0] - '0') {
        puts("-1");
    } else if (input[input.size() - 1] == '0') {
        puts("0");
    } else {
        if (r1 > r2) {
            for (int i = 0, r = 0; i < (int) input.size() && r < r1 - r2; i++) {
                if ((input[i] - '0') % 3 == 1) {
                    input.erase(input.begin() + i);
                    r++;
                    i--;
                }
            }
        } else if (r2 > r1) {
            for (int i = 0, r = 0; i < (int) input.size() && r < r2 - r1; i++) {
                if ((input[i] - '0') % 3 == 2) {
                    input.erase(input.begin() + i);
                    r++;
                    i--;
                }
            }
        }
        int sum = 0;
        for (int i = 0; i < (int) input.size(); i++) {
            sum += (input[i] - '0');
        }
        if (sum % 3 == 0) {
            for (int j = 0; j < (int) input.size(); j++)
                res = input[j] + res;
            cout << res << "\n";
        } else puts("-1");
    }
}


#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
    int n,flag=0,x,sum=0,m,count=0,count1=0,count2=0;
    cin>>n;
    int a[n+1],b[10]={0};
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        if(a[i]==0)
        {
        count++;
        flag=1;
        }

    }
    //cout<<"flag "<<flag<<endl;
//  cout<<"count is "<<count<<endl;

    if(flag==0)
    cout<<"-1"<<endl;
    else if(count==n)
    {
        cout<<0<<endl;
    }

    else
    {
        count=0;
        sort(a,a+n);
        for(int i=n-1;i>=0;i--)
        {
            sum+=a[i];
        }
        //cout<<"sum "<<sum<<endl;
        for(int i=0;i<n;i++)
        {
            b[a[i]]++;
        }
        m=(sum%3);
        /*
        for(int kk=0;kk<10;kk++)
        cout<<b[kk]<<" ";
        */
        //cout<<"m "<<m<<endl;
        if(m==0)
        {
            for(int i=n-1;i>=0;i--)
            {
                cout<<a[i];
            }
        }

        else
        {
            if(m==1)
            {
                for(int i=0;i<3;i++)
                {
                //cout<<"m "<<m<<endl;
                    if(b[m]!=0)
                    {
                        b[m]--;
                        count1++;
                        break;
                    }
                    m+=3;//cout<<"count1 "<<count1;
                }
                if(count1==0)
                {
                    m=2;
                    for(int i=0;i<3;i++)
                    {
                        if(b[m]!=0)
                        {
                            b[m]-2;
                            count2++;
                            break;
                        }
                        m+=3;
                    }
                }
            }

            else if(m==2)
            {
                for(int i=0;i<3;i++)
                {
                    if(b[m]!=0)
                    {
                        b[m]--;
                        count2++;
                        break;
                    }
                    m+=3;///cout<<"count2 "<<count2;
                }

                if(count2==0)
                {
                    //cout<<"lalit";
                    m=1;
                    for(int i=0;i<3;i++)
                    {
                        //cout<<"pathak "<<"m "<<m;
                        if(b[m]!=0)
                        {
                            //cout<<"b[m] "<<b[m]<<endl;
                            b[m]-=2;
                            break;
                            count2++;
                        }
                        m+=3;
                    }
                }
            }
            //for(int i=0;i<=9;i++)
            //cout<<"b[i] "<<b[i]<<" ";
            flag=false;
            for(int i=1;i<10;i++)
            {
                if(b[i]>0)
                flag=true;
            }

            if(flag==true)
            {
            for(int i=9;i>=0;i--)
            {
                if(b[i]!=0)
                {
                    x=i;
                    for(int j=0;j<b[i];j++)
                    cout<<x;
                }
            }
            }
            else
            cout<<0<<endl;
        }
    }
}


#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
    int n,flag=0,x,sum=0,m,count=0;
    cin>>n;
    int a[n+1],b[10]={0};
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        if(a[i]==0)
        flag=1;
    }
    //cout<<"flag "<<flag<<endl;
    if(flag==0)
    cout<<"-1"<<endl;
    else
    {
        sort(a,a+n);
        for(int i=n-1;i>=0;i--)
        {
            sum+=a[i];
        }
        //cout<<"sum "<<sum<<endl;
        for(int i=0;i<n;i++)
        {
            b[a[i]]++;
        }
        m=(sum%3);
        if(m==0)
        {
            for(int i=n-1;i>=0;i--)
            {
                cout<<a[i];
            }
        }
        else
        {
            if(m==1)
            {
                while(1)
                {
                    if(b[m]!=0)
                    {
                        b[m]--;
                        break;
                    }
                    m+=3;
                }
            }
            else if(m==2)
            {
                if(b[1]>=2)
                b[1]-=2;
            }
            //for(int i=0;i<=9;i++)
            //cout<<"b[i] "<<b[i]<<" ";
            for(int i=9;i>=0;i--)
            {
                if(b[i]!=0)
                {
                    x=i;
                    for(int j=0;j<b[i];j++)
                    cout<<x;
                }
            }
        }
    }
}


#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
    int n,flag=0,x,sum=0,m,count=0,count1=0,count2=0;
    cin>>n;
    int a[n+1],b[10]={0};
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        if(a[i]==0)
        {
        flag=1;
        count++;
    }
    }
    //cout<<"flag "<<flag<<endl;
    if(flag==0)
    cout<<"-1"<<endl;
    else if(count==n)
    {
        cout<<0<<endl;
        return 0;
    }

    else
    {
        sort(a,a+n);
        for(int i=n-1;i>=0;i--)
        {
            sum+=a[i];
        }
        //cout<<"sum "<<sum<<endl;
        for(int i=0;i<n;i++)
        {
            b[a[i]]++;
        }
        m=(sum%3);
        /*
        for(int kk=0;kk<10;kk++)
        cout<<b[kk]<<" ";
        */
        //cout<<"m "<<m<<endl;
        if(m==0)
        {
            for(int i=n-1;i>=0;i--)
            {
                cout<<a[i];
            }
        }

        else
        {
            if(m==1)
            {
                for(int i=0;i<3;i++)
                {
                //cout<<"m "<<m<<endl;
                    if(b[m]!=0)
                    {
                        b[m]--;
                        count1++;
                        break;
                    }
                    m+=3;//cout<<"count1 "<<count1;
                }
                if(count1==0)
                {
                    m=2;
                    for(int i=0;i<3;i++)
                    {
                        if(b[m]!=0)
                        {
                            b[m]-2;
                            count2++;
                            break;
                        }
                        m+=3;
                    }
                }
            }

            else if(m==2)
            {
                for(int i=0;i<3;i++)
                {
                    if(b[m]!=0)
                    {
                        b[m]--;
                        count2++;
                        break;
                    }
                    m+=3;///cout<<"count2 "<<count2;
                }

                if(count2==0)
                {
                    //cout<<"lalit";
                    m=1;
                    for(int i=0;i<3;i++)
                    {
                        //cout<<"pathak "<<"m "<<m;
                        if(b[m]!=0)
                        {
                            //cout<<"b[m] "<<b[m]<<endl;
                            b[m]-=2;
                            break;
                            count2++;
                        }
                        m+=3;
                    }
                }
            }
            //for(int i=0;i<=9;i++)
            //cout<<"b[i] "<<b[i]<<" ";
            for(int i=9;i>=0;i--)
            {
                if(b[i]!=0)
                {
                    x=i;
                    for(int j=0;j<b[i];j++)
                    cout<<x;
                }
            }
        }
    }
}


#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
    int n,flag=0,x,sum=0,m,count=0,count1=0,count2=0,temp=0;
    cin>>n;
    int a[n+1],b[10]={0};
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        if(a[i]==0)
        {
        count++;
        flag=1;
        }
    }

    if(flag==0)
    cout<<"-1"<<endl;
    else if(count==n)
    {
        cout<<0<<endl;
    }

    else
    {
        count=0;
        sort(a,a+n);
        for(int i=n-1;i>=0;i--)
        {
            sum+=a[i];
        }
        for(int i=0;i<n;i++)
        {
            b[a[i]]++;
        }
        m=(sum%3);
        if(m==0)
        {
            for(int i=n-1;i>=0;i--)
            {
                cout<<a[i];
            }
        }

        else
        {
            //cout<<"m "<<m<<endl;
            if(m==1)
            {
                for(int i=0;i<3;i++)
                {
                    if(b[m]!=0)
                    {
                        b[m]--;
                        count1++;
                        break;
                    }
                    m+=3;
                }
                if(count1==0)
                {
                    m=2;
                    for(int i=0;i<3;i++)
                    {
                        if(b[m]!=0)
                        {
                            if(b[m]>=2)
                            {
                                b[m]=b[m]-2;
                                break;
                            }
                            else
                            {
                                b[m]--;
                                temp++;
                                if(temp==2)
                                break;
                            }
                        }
                        m+=3;
                    }
                }
            }

            else if(m==2)
            {
                for(int i=0;i<3;i++)
                {
                    if(b[m]!=0)
                    {
                        b[m]--;
                        count2++;
                        break;
                    }
                    m+=3;
                }
                if(count2==0)
                {
                    m=1;
                    for(int i=0;i<3;i++)
                    {
                        if(b[m]!=0)
                        {
                            if(b[m]>=2)
                            {
                                b[m]=b[m]-2;
                                break;
                            }
                            else
                            {
                                b[m]--;
                                temp++;
                                if(temp==2)
                                break;
                            }
                        }
                        m+=3;
                    }
                }
            }
            flag=false;
            for(int i=1;i<10;i++)
            {
                if(b[i]>0)
                flag=true;
            }

            if(flag==true)
            {
            for(int i=9;i>=0;i--)
            {
                if(b[i]!=0)
                {
                    x=i;
                    for(int j=0;j<b[i];j++)
                    cout<<x;
                }
            }
            }
            else
            cout<<0<<endl;
        }
    }
}


#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
    int n,flag=0,x,sum=0,m,count=0,count1=0,count2=0;
    cin>>n;
    int a[n+1],b[10]={0};
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        if(a[i]==0)
        {
        count++;
        flag=1;
        }
    }

    if(flag==0)
    cout<<"-1"<<endl;
    else if(count==n)
    {
        cout<<0<<endl;
    }

    else
    {
        count=0;
        sort(a,a+n);
        for(int i=n-1;i>=0;i--)
        {
            sum+=a[i];
        }
        for(int i=0;i<n;i++)
        {
            b[a[i]]++;
        }
        m=(sum%3);
        if(m==0)
        {
            for(int i=n-1;i>=0;i--)
            {
                cout<<a[i];
            }
        }

        else
        {
        //  cout<<"m "<<m<<endl;
            if(m==1)
            {
                for(int i=0;i<3;i++)
                {
                    if(b[m]!=0)
                    {
                        b[m]--;
                        count1++;
                        break;
                    }
                    m+=3;
                }
                if(count1==0)
                {
                    //cout<<"lalit";
                    m=2;
                    for(int i=0;i<3;i++)
                    {
                        if(b[m]!=0)
                        {
                            b[m]=b[m]-2;
                            count2++;
                            //cout<<"b[m] "<<b[m]<<endl;
                            break;
                        }
                        m+=3;
                    }
                }
            }

            else if(m==2)
            {
                for(int i=0;i<3;i++)
                {
                    if(b[m]!=0)
                    {
                        b[m]--;
                        count2++;
                        break;
                    }
                    m+=3;
                }
                if(count2==0)
                {
                    m=1;
                    for(int i=0;i<3;i++)
                    {
                        if(b[m]!=0)
                        {
                            b[m]-=2;
                            break;
                            count2++;
                        }
                        m+=3;
                    }
                }
            }
            flag=false;
            for(int i=1;i<10;i++)
            {
                if(b[i]>0)
                flag=true;
            }

            if(flag==true)
            {
            for(int i=9;i>=0;i--)
            {
                if(b[i]!=0)
                {
                    x=i;
                    for(int j=0;j<b[i];j++)
                    cout<<x;
                }
            }
            }
            else
            cout<<0<<endl;
        }
    }
}


#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
    int n,flag=0,x,sum=0,m,count=0,count1=0,count2=0;
    cin>>n;
    int a[n+1],b[10]={0};
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        if(a[i]==0)
        {
        flag=1;
        count++;
    }
    }
    //cout<<"flag "<<flag<<endl;
    if(flag==0)
    cout<<"-1"<<endl;
    else if(count==n)
    {
        cout<<0<<endl;
    }

    else
    {
        count=0;
        sort(a,a+n);
        for(int i=n-1;i>=0;i--)
        {
            sum+=a[i];
        }
        //cout<<"sum "<<sum<<endl;
        for(int i=0;i<n;i++)
        {
            b[a[i]]++;
        }
        m=(sum%3);
        /*
        for(int kk=0;kk<10;kk++)
        cout<<b[kk]<<" ";
        */
        //cout<<"m "<<m<<endl;
        if(m==0)
        {
            for(int i=n-1;i>=0;i--)
            {
                cout<<a[i];
            }
        }

        else
        {
            if(m==1)
            {
                for(int i=0;i<3;i++)
                {
                //cout<<"m "<<m<<endl;
                    if(b[m]!=0)
                    {
                        b[m]--;
                        count1++;
                        break;
                    }
                    m+=3;//cout<<"count1 "<<count1;
                }
                if(count1==0)
                {
                    m=2;
                    for(int i=0;i<3;i++)
                    {
                        if(b[m]!=0)
                        {
                            b[m]-2;
                            count2++;
                            break;
                        }
                        m+=3;
                    }
                }
            }

            else if(m==2)
            {
                for(int i=0;i<3;i++)
                {
                    if(b[m]!=0)
                    {
                        b[m]--;
                        count2++;
                        break;
                    }
                    m+=3;///cout<<"count2 "<<count2;
                }

                if(count2==0)
                {
                    //cout<<"lalit";
                    m=1;
                    for(int i=0;i<3;i++)
                    {
                        //cout<<"pathak "<<"m "<<m;
                        if(b[m]!=0)
                        {
                            //cout<<"b[m] "<<b[m]<<endl;
                            b[m]-=2;
                            break;
                            count2++;
                        }
                        m+=3;
                    }
                }
            }
            //for(int i=0;i<=9;i++)
            //cout<<"b[i] "<<b[i]<<" ";
            for(int i=9;i>=0;i--)
            {
                if(b[i]!=0)
                {
                    x=i;
                    for(int j=0;j<b[i];j++)
                    cout<<x;
                }
            }
        }
    }
}


#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
    int n,flag=0,x,sum=0,m,count=0,count1=0,count2=0;
    cin>>n;
    int a[n+1],b[10]={0};
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        if(a[i]==0)
        {
        flag=1;
        count++;
    }
    }
    //cout<<"flag "<<flag<<endl;
    if(flag==0)
    cout<<"-1"<<endl;
    else if(count==n)
    {
        cout<<0<<endl;
        return 0;
    }

    else
    {
        count=0;
        sort(a,a+n);
        for(int i=n-1;i>=0;i--)
        {
            sum+=a[i];
        }
        //cout<<"sum "<<sum<<endl;
        for(int i=0;i<n;i++)
        {
            b[a[i]]++;
        }
        m=(sum%3);
        /*
        for(int kk=0;kk<10;kk++)
        cout<<b[kk]<<" ";
        */
        //cout<<"m "<<m<<endl;
        if(m==0)
        {
            for(int i=n-1;i>=0;i--)
            {
                cout<<a[i];
            }
        }

        else
        {
            if(m==1)
            {
                for(int i=0;i<3;i++)
                {
                //cout<<"m "<<m<<endl;
                    if(b[m]!=0)
                    {
                        b[m]--;
                        count1++;
                        break;
                    }
                    m+=3;//cout<<"count1 "<<count1;
                }
                if(count1==0)
                {
                    m=2;
                    for(int i=0;i<3;i++)
                    {
                        if(b[m]!=0)
                        {
                            b[m]-2;
                            count2++;
                            break;
                        }
                        m+=3;
                    }
                }
            }

            else if(m==2)
            {
                for(int i=0;i<3;i++)
                {
                    if(b[m]!=0)
                    {
                        b[m]--;
                        count2++;
                        break;
                    }
                    m+=3;///cout<<"count2 "<<count2;
                }

                if(count2==0)
                {
                    //cout<<"lalit";
                    m=1;
                    for(int i=0;i<3;i++)
                    {
                        //cout<<"pathak "<<"m "<<m;
                        if(b[m]!=0)
                        {
                            //cout<<"b[m] "<<b[m]<<endl;
                            b[m]-=2;
                            break;
                            count2++;
                        }
                        m+=3;
                    }
                }
            }
            //for(int i=0;i<=9;i++)
            //cout<<"b[i] "<<b[i]<<" ";
            for(int i=9;i>=0;i--)
            {
                if(b[i]!=0)
                {
                    x=i;
                    for(int j=0;j<b[i];j++)
                    cout<<x;
                }
            }
        }
    }
}


#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
    int n,flag=0,x,sum=0,m,count=0,count1=0,count2=0;
    cin>>n;
    int a[n+1],b[10]={0};
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        if(a[i]==0)
        flag=1;
    }
    //cout<<"flag "<<flag<<endl;
    if(flag==0)
    cout<<"-1"<<endl;
    else
    {
        sort(a,a+n);
        for(int i=n-1;i>=0;i--)
        {
            sum+=a[i];
        }
        //cout<<"sum "<<sum<<endl;
        for(int i=0;i<n;i++)
        {
            b[a[i]]++;
        }
        m=(sum%3);
        if(m==0)
        {
            for(int i=n-1;i>=0;i--)
            {
                cout<<a[i];
            }
        }
        else
        {
            if(m==1)
            {
                for(int i=0;i<3;i++)
                {
                    if(b[m]!=0)
                    {
                        b[m]--;
                        break;
                        count1++;
                    }
                    m+=3;//cout<<"count1 "<<count1;
                }
                if(count1==0)
                {
                    m=2;
                    for(int i=0;i<3;i++)
                    {
                        if(b[m]!=0)
                        {
                            b[m]--;
                            break;
                            count2++;
                        }
                        m+=3;
                    }
                }
            }
            else if(m==2)
            {
                for(int i=0;i<3;i++)
                {
                    if(b[m]!=0)
                    {
                        b[m]-=2;;
                        break;
                        count2++;
                    }
                    m+=3;///cout<<"count2 "<<count2;
                }
                if(count2==0)
                {
                    //cout<<"lalit";
                    m=1;
                    for(int i=0;i<3;i++)
                    {
                        //cout<<"pathak "<<"m "<<m;
                        if(b[m]!=0)
                        {
                            //cout<<"b[m] "<<b[m]<<endl;
                            b[m]-=2;
                            break;
                            count2++;
                        }
                        m+=3;
                    }
                }
            }
            //for(int i=0;i<=9;i++)
            //cout<<"b[i] "<<b[i]<<" ";
            for(int i=9;i>=0;i--)
            {
                if(b[i]!=0)
                {
                    x=i;
                    for(int j=0;j<b[i];j++)
                    cout<<x;
                }
            }
        }
    }
}


#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
    int n,flag=0,x,sum=0,m,count=0,count1=0,count2=0;
    cin>>n;
    int a[n+1],b[10]={0};
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        if(a[i]==0)
        {
        flag=1;
        }
        count++;
    }
    //cout<<"flag "<<flag<<endl;
    if(flag==0)
    cout<<"-1"<<endl;
    else if(count==n)
    {
        cout<<0<<endl;
    }

    else
    {
        count=0;
        sort(a,a+n);
        for(int i=n-1;i>=0;i--)
        {
            sum+=a[i];
        }
        //cout<<"sum "<<sum<<endl;
        for(int i=0;i<n;i++)
        {
            b[a[i]]++;
        }
        m=(sum%3);
        /*
        for(int kk=0;kk<10;kk++)
        cout<<b[kk]<<" ";
        */
        //cout<<"m "<<m<<endl;
        if(m==0)
        {
            for(int i=n-1;i>=0;i--)
            {
                cout<<a[i];
            }
        }

        else
        {
            if(m==1)
            {
                for(int i=0;i<3;i++)
                {
                //cout<<"m "<<m<<endl;
                    if(b[m]!=0)
                    {
                        b[m]--;
                        count1++;
                        break;
                    }
                    m+=3;//cout<<"count1 "<<count1;
                }
                if(count1==0)
                {
                    m=2;
                    for(int i=0;i<3;i++)
                    {
                        if(b[m]!=0)
                        {
                            b[m]-2;
                            count2++;
                            break;
                        }
                        m+=3;
                    }
                }
            }

            else if(m==2)
            {
                for(int i=0;i<3;i++)
                {
                    if(b[m]!=0)
                    {
                        b[m]--;
                        count2++;
                        break;
                    }
                    m+=3;///cout<<"count2 "<<count2;
                }

                if(count2==0)
                {
                    //cout<<"lalit";
                    m=1;
                    for(int i=0;i<3;i++)
                    {
                        //cout<<"pathak "<<"m "<<m;
                        if(b[m]!=0)
                        {
                            //cout<<"b[m] "<<b[m]<<endl;
                            b[m]-=2;
                            break;
                            count2++;
                        }
                        m+=3;
                    }
                }
            }
            //for(int i=0;i<=9;i++)
            //cout<<"b[i] "<<b[i]<<" ";
            for(int i=9;i>=0;i--)
            {
                if(b[i]!=0)
                {
                    x=i;
                    for(int j=0;j<b[i];j++)
                    cout<<x;
                }
            }
        }
    }
}


#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
    int n,flag=0,x,sum=0,m,count=0,count1=0,count2=0;
    cin>>n;
    int a[n+1],b[10]={0};
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        if(a[i]==0)
        {
        flag=1;
        count++;
    }
    }
    bool flg=false;
    //cout<<"flag "<<flag<<endl;
    if(count==0)
    cout<<"-1"<<endl;
    if(count==n)
    {
        cout<<0<<endl;
        flag=true;
    }

    else if(flg==false)
    {
        count=0;
        sort(a,a+n);
        for(int i=n-1;i>=0;i--)
        {
            sum+=a[i];
        }
        //cout<<"sum "<<sum<<endl;
        for(int i=0;i<n;i++)
        {
            b[a[i]]++;
        }
        m=(sum%3);
        /*
        for(int kk=0;kk<10;kk++)
        cout<<b[kk]<<" ";
        */
        //cout<<"m "<<m<<endl;
        if(m==0)
        {
            for(int i=n-1;i>=0;i--)
            {
                cout<<a[i];
            }
        }

        else
        {
            if(m==1)
            {
                for(int i=0;i<3;i++)
                {
                //cout<<"m "<<m<<endl;
                    if(b[m]!=0)
                    {
                        b[m]--;
                        count1++;
                        break;
                    }
                    m+=3;//cout<<"count1 "<<count1;
                }
                if(count1==0)
                {
                    m=2;
                    for(int i=0;i<3;i++)
                    {
                        if(b[m]!=0)
                        {
                            b[m]-2;
                            count2++;
                            break;
                        }
                        m+=3;
                    }
                }
            }

            else if(m==2)
            {
                for(int i=0;i<3;i++)
                {
                    if(b[m]!=0)
                    {
                        b[m]--;
                        count2++;
                        break;
                    }
                    m+=3;///cout<<"count2 "<<count2;
                }

                if(count2==0)
                {
                    //cout<<"lalit";
                    m=1;
                    for(int i=0;i<3;i++)
                    {
                        //cout<<"pathak "<<"m "<<m;
                        if(b[m]!=0)
                        {
                            //cout<<"b[m] "<<b[m]<<endl;
                            b[m]-=2;
                            break;
                            count2++;
                        }
                        m+=3;
                    }
                }
            }
            //for(int i=0;i<=9;i++)
            //cout<<"b[i] "<<b[i]<<" ";
            for(int i=9;i>=0;i--)
            {
                if(b[i]!=0)
                {
                    x=i;
                    for(int j=0;j<b[i];j++)
                    cout<<x;
                }
            }
        }
    }
}


#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
    int n,flag=0,x,sum=0,m,count=0,count1=0,count2=0;
    cin>>n;
    int a[n+1],b[10]={0};
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        if(a[i]==0)
        flag=1;
    }
    //cout<<"flag "<<flag<<endl;
    if(flag==0)
    cout<<"-1"<<endl;
    else
    {
        sort(a,a+n);
        for(int i=n-1;i>=0;i--)
        {
            sum+=a[i];
        }
        //cout<<"sum "<<sum<<endl;
        for(int i=0;i<n;i++)
        {
            b[a[i]]++;
        }
        m=(sum%3);
        //cout<<"m "<<m<<endl;
        if(m==0)
        {
            for(int i=n-1;i>=0;i--)
            {
                cout<<a[i];
            }
        }
        else
        {
            if(m==1)
            {
                for(int i=0;i<3;i++)
                {
                //cout<<"m "<<m<<endl;
                    if(b[m]!=0)
                    {
                        b[m]--;
                        count1++;
                        break;
                    }
                    m+=3;//cout<<"count1 "<<count1;
                }
                if(count1==0)
                {
                    m=2;
                    for(int i=0;i<3;i++)
                    {
                        if(b[m]!=0)
                        {
                            b[m]-2;
                            count2++;
                            break;
                        }
                        m+=3;
                    }
                }
            }
            else if(m==2)
            {
                for(int i=0;i<3;i++)
                {
                    if(b[m]!=0)
                    {
                        b[m]-=2;;
                        break;
                        count2++;
                    }
                    m+=3;///cout<<"count2 "<<count2;
                }
                if(count2==0)
                {
                    //cout<<"lalit";
                    m=1;
                    for(int i=0;i<3;i++)
                    {
                        //cout<<"pathak "<<"m "<<m;
                        if(b[m]!=0)
                        {
                            //cout<<"b[m] "<<b[m]<<endl;
                            b[m]-=2;
                            break;
                            count2++;
                        }
                        m+=3;
                    }
                }
            }
            //for(int i=0;i<=9;i++)
            //cout<<"b[i] "<<b[i]<<" ";
            for(int i=9;i>=0;i--)
            {
                if(b[i]!=0)
                {
                    x=i;
                    for(int j=0;j<b[i];j++)
                    cout<<x;
                }
            }
        }
    }
}


#include <string>
#include <list>
#include <vector>
#include <tr1/unordered_map>
#include <tr1/unordered_set>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string.h>
#include <memory.h>
#include <math.h>
#include <limits.h>
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
using namespace tr1;
typedef long long lint;
typedef unsigned long long ulint;

int a[200009];
int dp[200009][6];

vector<int> v;

int recurse(int n, int mod);
void reconstr(int n, int mod)
{
	if(n == 0)
	{
		if((a[n] + mod) % 3 != 0)
			return;
		v.push_back(a[0]);
		return;
	}

	int p1 = 1 + recurse(n - 1, (a[n] + mod) % 3);
	int p2 = recurse(n - 1, mod);

	if(p2 >= p1)
	{
		reconstr(n - 1, mod);
	}
	else
	{
		reconstr(n - 1, (a[n] + mod) % 3);
		v.push_back(a[n]);
	}
}
int main()
{
	int n;
	cin >> n;
	for(int i = 0; i < n; i++)
		scanf("%d", a + i);
	sort(a, a + n);
	reverse(a, a + n);

	if(a[n - 1] != 0)
	{
		printf("%d\n", -1);
		return 0;
	}

	if(a[0] == 0)
	{
		cout << 0 << endl;
		return 0;
	}

	memset(dp, 0xff, sizeof(dp));

	int val = recurse(n - 1, 0);

	//cout << val << endl;

	if(val < 0)
	{
		cout << 0 << endl;
		return 0;
	}

	reconstr(n - 1, 0);

	for(int i = 0; i < (int)v.size(); i++)
	{
		cout << v[i];
	}

	cout << endl;

	return 0;
}
int recurse(int n, int mod)
{
	if(n == 0)
	{
		if((a[n] + mod) % 3 != 0)
			return 0;
		if(mod != 0)
			return INT_MIN;
		return 1;
	}

	if(dp[n][mod] != -1)
		return dp[n][mod];

	int p1 = 1 + recurse(n - 1, (a[n] + mod) % 3);
	int p2 = recurse(n - 1, mod);

	return dp[n][mod] = max(p1, p2);

}


#include <string>
#include <list>
#include <vector>
#include <tr1/unordered_map>
#include <tr1/unordered_set>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string.h>
#include <memory.h>
#include <math.h>
#include <limits.h>
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
using namespace tr1;
typedef long long lint;
typedef unsigned long long ulint;

int a[200009];
int dp[200009][6];

vector<int> v;

int recurse(int n, int mod);
void reconstr(int n, int mod)
{
	if(n == 0)
	{
		if((a[n] + mod) % 3 != 0)
			return;
		v.push_back(a[0]);
		return;
	}

	int p1 = 1 + recurse(n - 1, (a[n] + mod) % 3);
	int p2 = recurse(n - 1, mod);

	if(p2 >= p1)
	{
		reconstr(n - 1, mod);
	}
	else
	{
		reconstr(n - 1, (a[n] + mod) % 3);
		v.push_back(a[n]);
	}
}
int main()
{
	int n;
	cin >> n;
	for(int i = 0; i < n; i++)
		scanf("%d", a + i);
	sort(a, a + n);
	reverse(a, a + n);

	if(a[n - 1] != 0)
	{
		printf("%d\n", -1);
		return 0;
	}

	if(a[0] == 0)
	{
		cout << 0 << endl;
		return 0;
	}

	memset(dp, 0xff, sizeof(dp));

	int val = recurse(n - 1, 0);

	if(val < 0)
	{
		cout << 0 << endl;
		return 0;
	}

	reconstr(n - 1, 0);

	for(int i = 0; i < (int)v.size(); i++)
	{
		cout << v[i];
	}

	cout << endl;

	return 0;
}
int recurse(int n, int mod)
{
	if(n == 0)
	{
		if((a[n] + mod) % 3 != 0)
			return INT_MIN;
		return 1;
	}

	if(dp[n][mod] != -1)
		return dp[n][mod];

	int p1 = 1 + recurse(n - 1, (a[n] + mod) % 3);
	int p2 = recurse(n - 1, mod);

	return dp[n][mod] = max(p1, p2);

}


#include <string>
#include <list>
#include <vector>
#include <tr1/unordered_map>
#include <tr1/unordered_set>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string.h>
#include <memory.h>
#include <math.h>
#include <limits.h>
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
using namespace tr1;
typedef long long lint;
typedef unsigned long long ulint;

int a[200009];
int dp[200009][6];

vector<int> v;

int recurse(int n, int mod);
void reconstr(int n, int mod)
{
	if(n == 0)
	{
		if(recurse(n, mod) == 1)
			v.push_back(a[0]);
		return;
	}

	int p1 = 1 + recurse(n - 1, (a[n] + mod) % 3);
	int p2 = recurse(n - 1, mod);

	if(p2 >= p1)
	{
		reconstr(n - 1, mod);
	}
	else
	{
		reconstr(n - 1, (a[n] + mod) % 3);
		v.push_back(a[n]);
	}
}
int main()
{
	int n;
	cin >> n;
	for(int i = 0; i < n; i++)
		scanf("%d", a + i);
	sort(a, a + n);
	reverse(a, a + n);

	if(a[n - 1] != 0)
	{
		printf("%d\n", -1);
		return 0;
	}

	if(a[0] == 0)
	{
		cout << 0 << endl;
		return 0;
	}

	memset(dp, 0xff, sizeof(dp));

	int val = recurse(n - 1, 0);

	//cout << val << endl;

	if(val < 0)
	{
		cout << 0 << endl;
		return 0;
	}

	reconstr(n - 1, 0);

	for(int i = 0; i < (int)v.size(); i++)
	{
		cout << v[i];
	}

	cout << endl;

	return 0;
}
int recurse(int n, int mod)
{

	if(n == 0)
	{
		int res = 100;
		if(mod != 0 && (a[n] + mod) % 3 != 0)
			return INT_MIN;
		if((a[n] + mod) % 3 != 0)
			return 0;
		//if(mod != 0)
			//return dp[n][mod] = INT_MIN;
		return 1;
	}

	if(dp[n][mod] != -1)
		return dp[n][mod];

	int p1 = 1 + recurse(n - 1, (a[n] + mod) % 3);
	int p2 = recurse(n - 1, mod);

	return dp[n][mod] = max(p1, p2);

}


#include <string>
#include <list>
#include <vector>
#include <tr1/unordered_map>
#include <tr1/unordered_set>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string.h>
#include <memory.h>
#include <math.h>
#include <limits.h>
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
using namespace tr1;
typedef long long lint;
typedef unsigned long long ulint;

int a[200009];
int dp[200009][6];

vector<int> v;

int recurse(int n, int mod);
void reconstr(int n, int mod)
{
	if(n == 0)
	{
		if((a[n] + mod) % 3 != 0)
			return;
		v.push_back(a[0]);
		return;
	}

	int p1 = 1 + recurse(n - 1, (a[n] + mod) % 3);
	int p2 = recurse(n - 1, mod);

	if(p2 >= p1)
	{
		reconstr(n - 1, mod);
	}
	else
	{
		reconstr(n - 1, (a[n] + mod) % 3);
		v.push_back(a[n]);
	}
}
int main()
{
	int n;
	cin >> n;
	for(int i = 0; i < n; i++)
		scanf("%d", a + i);
	sort(a, a + n);
	reverse(a, a + n);

	if(a[n - 1] != 0)
	{
		printf("%d\n", -1);
		return 0;
	}

	if(a[0] == 0)
	{
		cout << 0 << endl;
		return 0;
	}

	memset(dp, 0xff, sizeof(dp));

	recurse(n - 1, 0);

	reconstr(n - 1, 0);

	for(int i = 0; i < (int)v.size(); i++)
	{
		cout << v[i];
	}

	cout << endl;

	return 0;
}
int recurse(int n, int mod)
{
	if(n == 0)
	{
		if((a[n] + mod) % 3 != 0)
			return INT_MIN;
		return 1;
	}

	if(dp[n][mod] != -1)
		return dp[n][mod];

	int p1 = 1 + recurse(n - 1, (a[n] + mod) % 3);
	int p2 = recurse(n - 1, mod);

	return dp[n][mod] = max(p1, p2);

}


#include <string>
#include <list>
#include <vector>
#include <tr1/unordered_map>
#include <tr1/unordered_set>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string.h>
#include <memory.h>
#include <math.h>
#include <limits.h>
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
using namespace tr1;
typedef long long lint;
typedef unsigned long long ulint;

int a[100009];
int dp[100009][5];

vector<int> v;

int recurse(int n, int mod);
void reconstr(int n, int mod)
{
	if(n == 0)
	{
		if((a[n] + mod) % 3 != 0)
			return;
		v.push_back(a[0]);
		return;
	}

	int p1 = 1 + recurse(n - 1, (a[n] + mod) % 3);
	int p2 = recurse(n - 1, mod);

	if(p2 >= p1)
	{
		reconstr(n - 1, mod);
	}
	else
	{
		reconstr(n - 1, (a[n] + mod) % 3);
		v.push_back(a[n]);
	}
}
int main()
{
	int n;
	cin >> n;
	for(int i = 0; i < n; i++)
		scanf("%d", a + i);
	sort(a, a + n);
	reverse(a, a + n);

	if(a[n - 1] != 0)
	{
		printf("%d\n", -1);
		return 0;
	}

	memset(dp, 0xff, sizeof(dp));

	recurse(n - 1, 0);

	reconstr(n - 1, 0);

	if(v[0] == 0)
	{
		cout << 0 << endl;
		return 0;
	}

	for(int i = 0; i < (int)v.size(); i++)
	{
		cout << v[i];
	}

	cout << endl;

	return 0;
}
int recurse(int n, int mod)
{
	if(n == 0)
	{
		if((a[n] + mod) % 3 != 0)
			return 0;
		return 1;
	}

	if(dp[n][mod] != -1)
		return dp[n][mod];

	int p1 = 1 + recurse(n - 1, (a[n] + mod) % 3);
	int p2 = recurse(n - 1, mod);

	return dp[n][mod] = max(p1, p2);

}


#include <string>
#include <list>
#include <vector>
#include <tr1/unordered_map>
#include <tr1/unordered_set>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string.h>
#include <memory.h>
#include <math.h>
#include <limits.h>
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
using namespace tr1;
typedef long long lint;
typedef unsigned long long ulint;

int a[100009];
int dp[100009][5];

vector<int> v;

int recurse(int n, int mod);
void reconstr(int n, int mod)
{
	if(n == 0)
	{
		if((a[n] + mod) % 3 != 0)
			return;
		v.push_back(a[0]);
		return;
	}

	int p1 = 1 + recurse(n - 1, (a[n] + mod) % 3);
	int p2 = recurse(n - 1, mod);

	if(p2 > p1)
	{
		reconstr(n - 1, mod);
	}
	else
	{
		reconstr(n - 1, (a[n] + mod) % 3);
		v.push_back(a[n]);
	}
}
int main()
{
	int n;
	cin >> n;
	for(int i = 0; i < n; i++)
		scanf("%d", a + i);
	sort(a, a + n);
	reverse(a, a + n);

	if(a[n - 1] != 0)
	{
		printf("%d\n", -1);
		return 0;
	}

	int x;
	for(x = 0; x < n; x++)
	{
		if(a[x] != 0)
			break;
	}

	if(x == n)
	{
		cout << 0 << endl;
		return 0;
	}

	memset(dp, 0xff, sizeof(dp));

	recurse(n - 1, 0);

	reconstr(n - 1, 0);

	for(int i = 0; i < (int)v.size(); i++)
	{
		cout << v[i];
	}

	cout << endl;

	return 0;
}
int recurse(int n, int mod)
{
	if(n == 0)
	{
		if((a[n] + mod) % 3 != 0)
			return 0;
		return 1;
	}

	if(dp[n][mod] != -1)
		return dp[n][mod];

	int p1 = 1 + recurse(n - 1, (a[n] + mod) % 3);
	int p2 = recurse(n - 1, mod);

	return dp[n][mod] = max(p1, p2);

}


#include <iostream>
#include <string>
#include <list>
#include <vector>
#include <tr1/unordered_map>
#include <tr1/unordered_set>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string.h>
#include <memory.h>
#include <math.h>
#include <limits.h>
using namespace std;
using namespace tr1;
typedef long long lint;
typedef unsigned long long ulint;

lint bin[200][200];

#define MOD 1000000007LL

lint binom(int n, int k)
{
	if(n == 0 && k == 0) return 1;
	if(n < 0 || k < 0) return 0;
	if(n < k) return 0;
	if(k == 0) return bin[n][k] = 1;
	if(k == 1) return bin[n][k] = n;
	if(n == k) return bin[n][k] = 1;

	if(bin[n][k] != -1)
		return bin[n][k];

	return bin[n][k] = (binom(n - 1, k - 1) + binom(n - 1, k)) % MOD;
}

lint N;
lint a[15];

lint dp[1000][20];

lint f(lint n, lint index)
{
	if(n == 1 && index == 9 && a[9] > 1) return 0;
	if(n == 1 && index == 9)
		return 1;
	if(n == 0 && index > 9) return 1;
	if(index > 9) return 0;

	if(dp[n][index] != -1)
		return dp[n][index];

	//cout << n << " " << index << endl;

	lint sum = 0;
	for(int i = a[index]; i <= n; i++)
	{
		if(index != 0)
			sum += binom(n, i) * f(n - i, index + 1);
		else
			sum += binom(n - 1, i) * f(n - i, index + 1);
		sum %= MOD;
	}

	return dp[n][index] = sum;
}
int main()
{
	memset(bin, 0xff, sizeof(bin));
	binom(150, 150);

	cin >> N;
	for(int i = 0; i < 10; i++)
		cin >> a[i];

	memset(dp, 0xff, sizeof(dp));

	//cout << binom(3, 0) << endl;

	lint sum = 0;
	for(int n = N; n >= 1; n--)
	{
		//cout << "ok" << endl;

		sum += f(n, 0);
		sum %= MOD;
	}

	cout << sum << endl;

	return 0;
}


#include <string>
#include <list>
#include <vector>
#include <tr1/unordered_map>
#include <tr1/unordered_set>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string.h>
#include <memory.h>
#include <math.h>
#include <limits.h>
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
using namespace tr1;
typedef long long lint;
typedef unsigned long long ulint;

int a[200009];
int dp[200009][6];

vector<int> v;

int recurse(int n, int mod);
void reconstr(int n, int mod)
{
	if(n == 0)
	{
		if(recurse(n, mod) == 1)
			v.push_back(a[0]);
		return;
	}

	int p1 = 1 + recurse(n - 1, (a[n] + mod) % 3);
	int p2 = recurse(n - 1, mod);

	if(p2 >= p1)
	{
		reconstr(n - 1, mod);
	}
	else
	{
		reconstr(n - 1, (a[n] + mod) % 3);
		v.push_back(a[n]);
	}
}
int main()
{
	int n;
	cin >> n;
	for(int i = 0; i < n; i++)
		scanf("%d", a + i);
	sort(a, a + n);
	reverse(a, a + n);

	if(a[n - 1] != 0)
	{
		printf("%d\n", -1);
		return 0;
	}

	if(a[0] == 0)
	{
		cout << 0 << endl;
		return 0;
	}

	memset(dp, 0xff, sizeof(dp));

	int val = recurse(n - 1, 0);

	//cout << val << endl;

	if(val <= 0)
	{
		cout << 0 << endl;
		return 0;
	}

	if(v.size() == 0 || v[0] == 0)
	{
		cout << 0 << endl;
		return 0;
	}

	reconstr(n - 1, 0);

	for(int i = 0; i < (int)v.size(); i++)
	{
		cout << v[i];
	}

	cout << endl;

	return 0;
}
int recurse(int n, int mod)
{

	if(n == 0)
	{
		if(mod != 0 && (a[n] + mod) % 3 != 0)
			return INT_MIN;
		if((a[n] + mod) % 3 != 0)
			return 0;
		//if(mod != 0)
			//return dp[n][mod] = INT_MIN;
		return 1;
	}

	if(dp[n][mod] != -1)
		return dp[n][mod];

	int p1 = 1 + recurse(n - 1, (a[n] + mod) % 3);
	int p2 = recurse(n - 1, mod);

	return dp[n][mod] = max(p1, p2);

}


#include <string>
#include <list>
#include <vector>
#include <tr1/unordered_map>
#include <tr1/unordered_set>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string.h>
#include <memory.h>
#include <math.h>
#include <limits.h>
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
using namespace tr1;
typedef long long lint;
typedef unsigned long long ulint;

int a[200009];
int dp[200009][6];

vector<int> v;

int recurse(int n, int mod);
void reconstr(int n, int mod)
{
	if(n == 0)
	{
		if((a[n] + mod) % 3 != 0)
			return;
		v.push_back(a[0]);
		return;
	}

	int p1 = 1 + recurse(n - 1, (a[n] + mod) % 3);
	int p2 = recurse(n - 1, mod);

	if(p2 >= p1)
	{
		reconstr(n - 1, mod);
	}
	else
	{
		reconstr(n - 1, (a[n] + mod) % 3);
		v.push_back(a[n]);
	}
}
int main()
{
	int n;
	cin >> n;
	for(int i = 0; i < n; i++)
		scanf("%d", a + i);
	sort(a, a + n);
	reverse(a, a + n);

	if(a[n - 1] != 0)
	{
		printf("%d\n", -1);
		return 0;
	}

	if(a[0] == 0)
	{
		cout << 0 << endl;
		return 0;
	}

	memset(dp, 0xff, sizeof(dp));

	recurse(n - 1, 0);

	reconstr(n - 1, 0);

	if(v[0] == 0)
	{
		cout << 0 << endl;
		return 0;
	}

	for(int i = 0; i < (int)v.size(); i++)
	{
		cout << v[i];
	}

	cout << endl;

	return 0;
}
int recurse(int n, int mod)
{
	if(n == 0)
	{
		if((a[n] + mod) % 3 != 0)
			return INT_MIN;
		return 1;
	}

	if(dp[n][mod] != -1)
		return dp[n][mod];

	int p1 = 1 + recurse(n - 1, (a[n] + mod) % 3);
	int p2 = recurse(n - 1, mod);

	return dp[n][mod] = max(p1, p2);

}


#include <string>
#include <list>
#include <vector>
#include <tr1/unordered_map>
#include <tr1/unordered_set>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string.h>
#include <memory.h>
#include <math.h>
#include <limits.h>
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
using namespace tr1;
typedef long long lint;
typedef unsigned long long ulint;

int a[100009];
int dp[100009][5];

vector<int> v;

int recurse(int n, int mod);
void reconstr(int n, int mod)
{
	if(n == 0)
	{
		if((a[n] + mod) % 3 != 0)
			return;
		v.push_back(a[0]);
		return;
	}

	int p1 = 1 + recurse(n - 1, (a[n] + mod) % 3);
	int p2 = recurse(n - 1, mod);

	if(p2 >= p1)
	{
		reconstr(n - 1, mod);
	}
	else
	{
		reconstr(n - 1, (a[n] + mod) % 3);
		v.push_back(a[n]);
	}
}
int main()
{
	int n;
	cin >> n;
	for(int i = 0; i < n; i++)
		scanf("%d", a + i);
	sort(a, a + n);
	reverse(a, a + n);

	if(a[n - 1] != 0)
	{
		printf("%d\n", -1);
		return 0;
	}

	if(a[0] == 0)
	{
		cout << 0 << endl;
		return 0;
	}

	memset(dp, 0xff, sizeof(dp));

	recurse(n - 1, 0);

	reconstr(n - 1, 0);

	for(int i = 0; i < (int)v.size(); i++)
	{
		cout << v[i];
	}

	cout << endl;

	return 0;
}
int recurse(int n, int mod)
{
	if(n == 0)
	{
		if((a[n] + mod) % 3 != 0)
			return INT_MIN;
		return 1;
	}

	if(dp[n][mod] != -1)
		return dp[n][mod];

	int p1 = 1 + recurse(n - 1, (a[n] + mod) % 3);
	int p2 = recurse(n - 1, mod);

	return dp[n][mod] = max(p1, p2);

}


#include <string>
#include <list>
#include <vector>
#include <tr1/unordered_map>
#include <tr1/unordered_set>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string.h>
#include <memory.h>
#include <math.h>
#include <limits.h>
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
using namespace tr1;
typedef long long lint;
typedef unsigned long long ulint;

int a[100009];
int dp[100009][5];

vector<int> v;

int recurse(int n, int mod);
void reconstr(int n, int mod)
{
	if(n == 0)
	{
		if((a[n] + mod) % 3 != 0)
			return;
		v.push_back(a[0]);
		return;
	}

	int p1 = 1 + recurse(n - 1, (a[n] + mod) % 3);
	int p2 = recurse(n - 1, mod);

	if(p2 >= p1)
	{
		reconstr(n - 1, mod);
	}
	else
	{
		reconstr(n - 1, (a[n] + mod) % 3);
		v.push_back(a[n]);
	}
}
int main()
{
	int n;
	cin >> n;
	for(int i = 0; i < n; i++)
		scanf("%d", a + i);
	sort(a, a + n);
	reverse(a, a + n);

	if(a[n - 1] != 0)
	{
		printf("%d\n", -1);
		return 0;
	}

	int x;
	for(x = 0; x < n; x++)
	{
		if(a[x] != 0)
			break;
	}

	if(x == n)
	{
		cout << 0 << endl;
		return 0;
	}

	memset(dp, 0xff, sizeof(dp));

	recurse(n - 1, 0);

	reconstr(n - 1, 0);

	for(int i = 0; i < (int)v.size(); i++)
	{
		cout << v[i];
	}

	cout << endl;

	return 0;
}
int recurse(int n, int mod)
{
	if(n == 0)
	{
		if((a[n] + mod) % 3 != 0)
			return INT_MIN;
		return 1;
	}

	if(dp[n][mod] != -1)
		return dp[n][mod];

	int p1 = 1 + recurse(n - 1, (a[n] + mod) % 3);
	int p2 = recurse(n - 1, mod);

	return dp[n][mod] = max(p1, p2);

}


#include <string>
#include <list>
#include <vector>
#include <tr1/unordered_map>
#include <tr1/unordered_set>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string.h>
#include <memory.h>
#include <math.h>
#include <limits.h>
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
using namespace tr1;
typedef long long lint;
typedef unsigned long long ulint;

int a[100009];
int dp[100009][5];

vector<int> v;

int recurse(int n, int mod);
void reconstr(int n, int mod)
{
	if(n == 0)
	{
		if((a[n] + mod) % 3 != 0)
			return;
		v.push_back(a[0]);
		return;
	}

	int p1 = 1 + recurse(n - 1, (a[n] + mod) % 3);
	int p2 = recurse(n - 1, mod);

	if(p2 >= p1)
	{
		reconstr(n - 1, mod);
	}
	else
	{
		reconstr(n - 1, (a[n] + mod) % 3);
		v.push_back(a[n]);
	}
}
int main()
{
	int n;
	cin >> n;
	for(int i = 0; i < n; i++)
		scanf("%d", a + i);
	sort(a, a + n);
	reverse(a, a + n);

	if(a[n - 1] != 0)
	{
		printf("%d\n", -1);
		return 0;
	}

	if(a[0] == 0)
	{
		cout << "xxx" << endl;

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

	memset(dp, 0xff, sizeof(dp));

	recurse(n - 1, 0);

	reconstr(n - 1, 0);

	for(int i = 0; i < (int)v.size(); i++)
	{
		cout << v[i];
	}

	cout << endl;

	return 0;
}
int recurse(int n, int mod)
{
	if(n == 0)
	{
		if((a[n] + mod) % 3 != 0)
			return INT_MIN;
		return 1;
	}

	if(dp[n][mod] != -1)
		return dp[n][mod];

	int p1 = 1 + recurse(n - 1, (a[n] + mod) % 3);
	int p2 = recurse(n - 1, mod);

	return dp[n][mod] = max(p1, p2);

}


#include <string>
#include <list>
#include <vector>
#include <tr1/unordered_map>
#include <tr1/unordered_set>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string.h>
#include <memory.h>
#include <math.h>
#include <limits.h>
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
using namespace tr1;
typedef long long lint;
typedef unsigned long long ulint;

int a[200009];
int dp[200009][6];

vector<int> v;

int recurse(int n, int mod);
void reconstr(int n, int mod)
{
	if(n == 0)
	{
		if(recurse(n, mod) == 1)
			v.push_back(a[0]);
		return;
	}

	int p1 = 1 + recurse(n - 1, (a[n] + mod) % 3);
	int p2 = recurse(n - 1, mod);

	if(p2 >= p1)
	{
		reconstr(n - 1, mod);
	}
	else
	{
		reconstr(n - 1, (a[n] + mod) % 3);
		v.push_back(a[n]);
	}
}
int main()
{
	int n;
	cin >> n;
	for(int i = 0; i < n; i++)
		scanf("%d", a + i);
	sort(a, a + n);
	reverse(a, a + n);

	if(a[n - 1] != 0)
	{
		printf("%d\n", -1);
		return 0;
	}

	if(a[0] == 0)
	{
		cout << 0 << endl;
		return 0;
	}

	memset(dp, 0xff, sizeof(dp));

	int val = recurse(n - 1, 0);

	//cout << val << endl;

	if(val < 0)
	{
		cout << 0 << endl;
		return 0;
	}

	reconstr(n - 1, 0);

	if(v.size() == 0 || v[0] == 0)
	{
		cout << 0 << endl;
		return 0;
	}

	for(int i = 0; i < (int)v.size(); i++)
	{
		cout << v[i];
	}

	cout << endl;

	return 0;
}
int recurse(int n, int mod)
{

	if(n == 0)
	{
		if(mod != 0 && (a[n] + mod) % 3 != 0)
			return INT_MIN;
		if((a[n] + mod) % 3 != 0)
			return 0;
		//if(mod != 0)
			//return dp[n][mod] = INT_MIN;
		return 1;
	}

	if(dp[n][mod] != -1)
		return dp[n][mod];

	int p1 = 1 + recurse(n - 1, (a[n] + mod) % 3);
	int p2 = recurse(n - 1, mod);

	return dp[n][mod] = max(p1, p2);

}