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

██╗    ██╗██████╗ ██╗████████╗████████╗███████╗███╗   ██╗    ██████╗ ██╗   ██╗
██║    ██║██╔══██╗██║╚══██╔══╝╚══██╔══╝██╔════╝████╗  ██║    ██╔══██╗╚██╗ ██╔╝
██║ █╗ ██║██████╔╝██║   ██║      ██║   █████╗  ██╔██╗ ██║    ██████╔╝ ╚████╔╝
██║███╗██║██╔══██╗██║   ██║      ██║   ██╔══╝  ██║╚██╗██║    ██╔══██╗  ╚██╔╝
╚███╔███╔╝██║  ██║██║   ██║      ██║   ███████╗██║ ╚████║    ██████╔╝   ██║
 ╚══╝╚══╝ ╚═╝  ╚═╝╚═╝   ╚═╝      ╚═╝   ╚══════╝╚═╝  ╚═══╝    ╚═════╝    ╚═╝

     ███╗   ███╗   ██╗██████╗ ██████╗  █████╗ ██╗  ██╗██╗███╗   ███╗
     ████╗ ████║   ██║██╔══██╗██╔══██╗██╔══██╗██║  ██║██║████╗ ████║
     ██╔████╔██║   ██║██████╔╝██████╔╝███████║███████║██║██╔████╔██║
     ██║╚██╔╝██║   ██║██╔══██╗██╔══██╗██╔══██║██╔══██║██║██║╚██╔╝██║
     ██║ ╚═╝ ██║██╗██║██████╔╝██║  ██║██║  ██║██║  ██║██║██║ ╚═╝ ██║
     ╚═╝     ╚═╝╚═╝╚═╝╚═════╝ ╚═╝  ╚═╝╚═╝  ╚═╝╚═╝  ╚═╝╚═╝╚═╝     ╚═╝
*/
#define speed  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define FOR(n) for(int i=0;i<n;i++)
#define For(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define E "\n"
#define all(e) e.begin(),e.end()
#define F first
#define S second
#define rad(x) ((x*pi)/(ld)(180))
#define perc(x) fixed<<setprecision(x)
typedef long long int ll; typedef unsigned long long ull; typedef long double ld;typedef complex<ld> point; typedef tuple<double,double,double> line;
ll lcm(ll x,ll y){return (x*y)/__gcd(x,y);};
const int N = 2e7+1;
priority_queue<int, vector<int> , greater<int> > pq;
ll n,x,a1=1,a2=1e6;

int main(){speed;
cin>>n;
FOR(n)
{
    cin>>x;
    if(abs(a1-x)<=abs(a2-x))
        a1=x;
    else a2 = x;
}

cout<<max(abs(a1-1),abs(int(1e6)-a2));
return 0;}


#include <bits/stdc++.h>

using namespace std;
#define speed ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
typedef long long int ll;

int main(){
    speed;
int n;
cin>>n;
int p1=1,p2=1000000;
int ans=0;
for(int i=0;i<n;i++)
{
    int x;
cin>>x;
if(x-1<1000000-x){p1=x;}
else if(x-1>=100000-x){p2=x;}
}
p1=p1-1;
p2=1000000-p2;
ans=max(p1,p2);
cout<<ans<<endl;
    return 0;
}
//if(){}


#include<bits/stdc++.h>

using namespace std;
/*
                            ببِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم

██╗    ██╗██████╗ ██╗████████╗████████╗███████╗███╗   ██╗    ██████╗ ██╗   ██╗
██║    ██║██╔══██╗██║╚══██╔══╝╚══██╔══╝██╔════╝████╗  ██║    ██╔══██╗╚██╗ ██╔╝
██║ █╗ ██║██████╔╝██║   ██║      ██║   █████╗  ██╔██╗ ██║    ██████╔╝ ╚████╔╝
██║███╗██║██╔══██╗██║   ██║      ██║   ██╔══╝  ██║╚██╗██║    ██╔══██╗  ╚██╔╝
╚███╔███╔╝██║  ██║██║   ██║      ██║   ███████╗██║ ╚████║    ██████╔╝   ██║
 ╚══╝╚══╝ ╚═╝  ╚═╝╚═╝   ╚═╝      ╚═╝   ╚══════╝╚═╝  ╚═══╝    ╚═════╝    ╚═╝

     ███╗   ███╗   ██╗██████╗ ██████╗  █████╗ ██╗  ██╗██╗███╗   ███╗
     ████╗ ████║   ██║██╔══██╗██╔══██╗██╔══██╗██║  ██║██║████╗ ████║
     ██╔████╔██║   ██║██████╔╝██████╔╝███████║███████║██║██╔████╔██║
     ██║╚██╔╝██║   ██║██╔══██╗██╔══██╗██╔══██║██╔══██║██║██║╚██╔╝██║
     ██║ ╚═╝ ██║██╗██║██████╔╝██║  ██║██║  ██║██║  ██║██║██║ ╚═╝ ██║
     ╚═╝     ╚═╝╚═╝╚═╝╚═════╝ ╚═╝  ╚═╝╚═╝  ╚═╝╚═╝  ╚═╝╚═╝╚═╝     ╚═╝
*/
#define file freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
#define speed ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define FOR(n) for(int i=0;i<n;i++)
#define For(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define E endl
#define all(e) e.begin(),e.end()
#define mp(x,y) make_pair(x,y)
#define F first
#define S second
const int MOD=1e9+9;typedef long int L;typedef long long int ll;

int main(){//speed;
int n,x,ans=0; scanf("%d",&n);
FOR(n){scanf("%d",&x); ans=max(ans, min(x - 1, 1000000 - x)); }
printf("%d",ans);
 return 0; }


#include<bits/stdc++.h>
using namespace std;
int ToInt(string s)
{
    int r = 0;
    stringstream sin(s);
    sin >> r;
    return r;
}
string ToString(int n)
{
    string s;
    stringstream convert;
    convert << n;
    s = convert.str();
    return s;
}
int arr[ 1000000 + 100 ] ;

int main()
{
    int n ;
    cin >> n ;
    for( int i = 0 ; i < n ; i ++ )
    {
        int a ;
        cin >> a ;
      arr[ a ] = 1 ;
    }

    int x = 2 , y = 1e6 - 1  ;
    int ok = 0 , ans = 0 ;
   for( int i = 2 ; i  <= 100000 ; i ++ ){
    if ( ok >= n ) break ;
    if ( arr[ i ] == 1 || arr[ y ] == 1 ) ok ++ ;
    y -- ;
    ans ++ ;

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

}


#include<bits/stdc++.h>
using namespace std;
int ToInt(string s)
{
    int r = 0;
    stringstream sin(s);
    sin >> r;
    return r;
}
string ToString(int n)
{
    string s;
    stringstream convert;
    convert << n;
    s = convert.str();
    return s;
}
vector < int > arr ;

int main()
{
    int n ;
    cin >> n ;
    for( int i = 0 ; i < n ; i ++ )
    {
        int a ;
        cin >> a ;
        arr.push_back ( a) ;
    }
    sort(arr.begin(),arr.end());
    int ans = 0 ;
    int x = 1 , y = 1e6 ;
    int i = 0 , j = n - 1 ;
    while ( i <= j )
    {
        int xx = abs ( arr[ i ] - x ) ;
        int xxx = abs ( arr[ i ] - y ) ;
        int yy = abs(arr[j] - x) ;
        int yyy = abs ( arr[ j ] - y ) ;
        int tempx = x , tempy = y ;
        if( xx < xxx ){
           x = arr[ i ] ;
        }else {
          y = arr [ i ] ;
        }
        if ( tempx == x ){
          if ( yy < yyy ){
            ans += max(min(xx,xxx),min(yy,yyy));
             x = arr[ j ] ;
            i ++ , j -- ;
          }else {
            ans += xxx ;
            j -- ;
          }
        }else {
          if ( yy > yyy ){
           ans += max(min(xx,xxx),min(yy,yyy));
            y = arr[ j ];
           i ++ , j -- ;
          }else {
            ans += xx ;
            i ++ ;
          }
        }

    }
    cout << ans << endl;

    return 0 ;

}


#include<bits/stdc++.h>
using namespace std;
int ToInt(string s)
{
    int r = 0;
    stringstream sin(s);
    sin >> r;
    return r;
}
string ToString(int n)
{
    string s;
    stringstream convert;
    convert << n;
    s = convert.str();
    return s;
}
int arr[ 1000000 + 100 ] ;

int main()
{
    int n ;
    cin >> n ;
    for( int i = 0 ; i < n ; i ++ )
    {
        int a ;
        cin >> a ;
        arr[ a ] = 1 ;
    }

    int x = 2 , y = 1e6 - 1  ;
    int ok = 0 , ans = 0 ;
    for( int i = 2 ; i  <= 1000000 ; i ++ )
    {
        if ( ok >= n ) break ;
        if ( arr[ i ] == 1 || arr[ y ] == 1 ) ok ++ ;
        y -- ;
        ans ++ ;

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

}


#include<bits/stdc++.h>
using namespace std;
int ToInt(string s)
{
    int r = 0;
    stringstream sin(s);
    sin >> r;
    return r;
}
string ToString(int n)
{
    string s;
    stringstream convert;
    convert << n;
    s = convert.str();
    return s;
}
vector < int > arr ;

int main()
{
    int n ;
    cin >> n ;
    for( int i = 0 ; i < n ; i ++ )
    {
        int a ;
        cin >> a ;
        arr.push_back ( a) ;
    }
    sort(arr.begin(),arr.end());
    int ans = 0 ;
    int x = 1 , y = 1e6 ;
    int i = 0 , j = n - 1 ;
    while ( i <= j )
    {
        int xx = abs ( arr[ i ] - x ) ;
        int xxx = abs ( arr[ i ] - y ) ;
        int yy = abs(arr[j] - x) ;
        int yyy = abs ( arr[ j ] - y ) ;
        int tempx = x , tempy = y ;
        if( xx < xxx ){
           x = arr[ i ] ;
        }else {
          y = arr [ i ] ;
        }
        if ( tempx == x ){
          if ( yy > yyy ){
            ans += max(min(xx,xxx),min(yy,yyy));
             x = arr[ j ] ;
            i ++ , j -- ;
          }else {
            ans += xxx ;
            j -- ;
          }
        }else {
          if ( yy > yyy ){
           ans += max(min(xx,xxx),min(yy,yyy));
            y = arr[ j ];
           i ++ , j -- ;
          }else {
            ans += xx ;
            i ++ ;
          }
        }

    }
    cout << ans << endl;

    return 0 ;

}


#include<bits/stdc++.h>
using namespace std;
int ToInt(string s)
{
    int r = 0;
    stringstream sin(s);
    sin >> r;
    return r;
}
string ToString(int n)
{
    string s;
    stringstream convert;
    convert << n;
    s = convert.str();
    return s;
}
vector < int > arr ;

int main()
{
    int n ;
    cin >> n ;
    for( int i = 0 ; i < n ; i ++ )
    {
        int a ;
        cin >> a ;
        arr.push_back ( a) ;
    }
    sort(arr.begin(),arr.end());
    int ans = 0 ;
    int x = 1 , y = 1e6 ;
    int i = 0 , j = n - 1 ;
    while ( i <= j )
    {
        int xx = abs ( arr[ i ] - x ) ;
        int xxx = abs ( arr[ i ] - y ) ;
        int yy = abs(arr[j] - x) ;
        int yyy = abs ( arr[ j ] - y ) ;
        // cout << xx << " " << xxx << " " << yy << " " <<yyy << endl;

        //  ans += max(min(xx,xxx), min(yy,yyy));
        //  cout << ans << endl;
        int cnt = 0 ;
        int cntt = 0 ;

        if ( xx > xxx )
        {
            y = arr[i] ;
            cntt ++ ;

        }
        else
        {
            x = arr[ i] ;
            cnt ++ ;
        }

        if ( yy > yyy )
        {
            y = arr[ j ];
            cntt ++ ;
        }
        else
        {
            x= arr[j ];
            cnt ++ ;
        }

        if ( cnt != 2 && cntt !=2 )
        {
            ans += max(min(xx,xxx), min(yy,yyy));

            i ++ ;
            j -- ;
        }
        if ( cnt == 2 && cntt == 0 )
        {

            ans += min(xx,xxx);

            if ( xx > xxx )
            {
                y = arr[i] ;
                cntt ++ ;

            }
            else
            {
                x = arr[ i] ;
                cnt ++ ;
            }

            i ++ ;
        }
        if ( cntt == 2 && cnt == 0 )
        {
            ;
            ans += min(yy,yyy);
            if ( yy > yyy )
            {
                y = arr[ j ];
                cntt ++ ;
            }
            else
            {
                x= arr[j ];
                cnt ++ ;
            }
            j-- ;
        }

        //  cout << x <<" " << y << endl;

    }
    cout << ans << endl;

    return 0 ;

}


#include<bits/stdc++.h>
using namespace std;
int ToInt(string s)
{
    int r = 0;
    stringstream sin(s);
    sin >> r;
    return r;
}
string ToString(int n)
{
    string s;
    stringstream convert;
    convert << n;
    s = convert.str();
    return s;
}
int arr[ 1000000 + 100 ] ;

int main()
{
    int n ;
    cin >> n ;
    for( int i = 0 ; i < n ; i ++ )
    {
        int a ;
        cin >> a ;
      arr[ a ] = 1 ;
    }

    int x = 2 , y = 1e6 - 1  ;
    int ok = 0 , ans = 0 ;
   for( int i = 2 ; i  <= 10000000 ; i ++ ){
    if ( ok >= n ) break ;
    if ( arr[ i ] == 1  ) ok ++ ;
    if ( arr[ y ] == 1 ) ok ++ ;
    y -- ;
    ans ++ ;

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

}


#include<bits/stdc++.h>
using namespace std;
int ToInt(string s)
{
    int r = 0;
    stringstream sin(s);
    sin >> r;
    return r;
}
string ToString(int n)
{
    string s;
    stringstream convert;
    convert << n;
    s = convert.str();
    return s;
}
int arr[ 1000000 + 100 ] ;

int main()
{
    int n ;
    cin >> n ;
    for( int i = 0 ; i < n ; i ++ )
    {
        int a ;
        cin >> a ;
      arr[ a ] = 1 ;
    }

    int x = 2 , y = 1e6 - 1  ;
    int ok = 0 , ans = 0 ;
   for( int i = 2 ; i  <= 10000000 ; i ++ ){
    if ( ok >= n ) break ;
    if ( arr[ i ] == 1 || arr[ y ] == 1 ) ok ++ ;
    y -- ;
    ans ++ ;

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

}


#include<bits/stdc++.h>
using namespace std;
int ToInt(string s)
{
    int r = 0;
    stringstream sin(s);
    sin >> r;
    return r;
}
string ToString(int n)
{
    string s;
    stringstream convert;
    convert << n;
    s = convert.str();
    return s;
}
vector < int > arr ;

int main()
{
    int n ;
    cin >> n ;
    for( int i = 0 ; i < n ; i ++ )
    {
        int a ;
        cin >> a ;
        arr.push_back ( a) ;
    }
    sort(arr.begin(),arr.end());
    int ans = 0 ;
    int x = 1 , y = 1e6 ;
    int i = 0 , j = n - 1 ;
    while ( i <= j )
    {
        int xx = abs ( arr[ i ] - x ) ;
        int xxx = abs ( arr[ i ] - y ) ;
        int yy = abs(arr[j] - x) ;
        int yyy = abs ( arr[ j ] - y ) ;
        if ( xx < xxx ){
            if ( yy > yyy ){
                ans += max(min(xx,xxx),min(yy,yyy));
                x = arr[i];
                y = arr[j];
                i ++ ; j -- ;
            }else {
                ans += xx ;
                x = arr[ i ];
                i ++ ;
            }
        }
        //cut << ans << endl ;

    }
    cout << ans << endl;

    return 0 ;

}


#include<bits/stdc++.h>
using namespace std;
int ToInt(string s)
{
    int r = 0;
    stringstream sin(s);
    sin >> r;
    return r;
}
string ToString(int n)
{
    string s;
    stringstream convert;
    convert << n;
    s = convert.str();
    return s;
}
vector < int > arr ;

int main()
{
    int n ;
    cin >> n ;
    for( int i = 0 ; i < n ; i ++ )
    {
        int a ;
        cin >> a ;
        arr.push_back ( a) ;
    }
    sort(arr.begin(),arr.end());
    int ans = 0 ;
    int x = 1 , y = 1e6 ;
    int i = 0 , j = n - 1 ;
    while ( i <= j )
    {
        int xx = abs ( arr[ i ] - x ) ;
        int xxx = abs ( arr[ i ] - y ) ;
        int yy = abs(arr[j] - x) ;
        int yyy = abs ( arr[ j ] - y ) ;
        int tempx = x , tempy = y ;
        if( xx < xxx ){
           x = arr[ i ] ;
        }else {
          y = arr [ i ] ;
        }
        if ( tempx == x ){
          if ( yy <= yyy ){
            ans += max(min(xx,xxx),min(yy,yyy));
             x = arr[ j ] ;
            i ++ , j -- ;
          }else {
            ans += xxx ;
            j -- ;
          }
        }else {
          if ( yy >= yyy ){
           ans += max(min(xx,xxx),min(yy,yyy));
            y = arr[ j ];
           i ++ , j -- ;
          }else {
            ans += xx ;
            i ++ ;
          }
        }

    }
    cout << ans << endl;

    return 0 ;

}


#include <iostream>
#include <bits/stdc++.h>
// *__*

typedef long long ll;
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const double pi = 3.14159265;
const ll mod=1000000007;
using namespace std;

int main()
{fast //ll t;cin>>t;while(t--)
{
	ll x , y ,z=0 , hi =0 ,r=0, kkk = 1000000 ,o,m,a=0,b=0,c,d, mn = 1000000,k=0,kk=0,ans,mn1=mn,mn2=0;

	cin >> x ;

	for(ll i = 0 ; i < x ;i++)
    {
        cin >> y ;

        if(y<mn)mn=y;
        if(y>k)k=y;
        if(y>500000&&y<kkk){kkk=y;a=1;}
        if(y<=500000&&y>mn2){mn2=y;b=1;}
    }
    mn++;k--;
    k = k ; // ماكس من تحت
    mn = 1000000-mn ; // ماكس من فوق

    d = min(mn,k);

    if(a==1&&b==1)
    {
    mn2--;
    kkk = 1000000-(kkk);
   // cout << kkk <<endl;
    o= max(kkk,mn2);
    }

   // cout << o << " " << kk << " " << mn <<endl;
    d = min(d,o);

    cout << d <<endl;

}
}


#include <iostream>
#include <bits/stdc++.h>
// *__*

typedef long long ll;
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const double pi = 3.14159265;
const ll mod=1000000007;
using namespace std;

int main()
{fast //ll t;cin>>t;while(t--)
{
	ll x , y ,z=0 , hi =0 ,r=0, kkk = 1000000 ,o,m,a,b,c,d, mn = 1000000,k=0,kk=0,ans,mn1=mn,mn2=0;

	cin >> x ;

	for(int i = 0 ; i < x ;i++)
    {
        cin >> y ;

        if(y<mn)mn=y;
        if(y>k)k=y;
        if(y>500000&&y<kkk)kkk=y;
        if(y<=500000&&y>mn2)mn2=y-1;
    }

    kkk = 1000000-(kkk+1);
    o = kkk + mn2 ;
    kk = k -1 ; // ماكس من تحت
    mn1 = 1000000-mn ; // ماكس من فوق

    d = min(kk,o);
    d = min(d,mn1);

    cout << d <<endl;

}
}


#include <iostream>
#include <bits/stdc++.h>
// *__*

typedef long long ll;
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const double pi = 3.14159265;
const ll mod=1000000007;
using namespace std;

int main()
{fast //ll t;cin>>t;while(t--)
{
	ll x , y ,z=0 , hi =0 ,r=0, kkk = 1000000 ,o,m,a,b,c,d, mn = 1000000,k=0,kk=0,ans,mn1=mn,mn2=0;

	cin >> x ;

	for(int i = 0 ; i < x ;i++)
    {
        cin >> y ;

        if(y<mn)mn=y;
        if(y>k)k=y;
        if(y>500000&&y<kkk)kkk=y;
        if(y<=500000&&y>mn2)mn2=y-1;
    }

    kkk = 1000000-(kkk+1);
    o = (kkk+1) + mn2 ;
    kk = k -1 ; // ماكس من تحت
    mn1 = 1000000-mn ; // ماكس من فوق

   // cout << o << " " << kk << " " << mn1 <<endl;
    d = min(kk,o);
    d = min(d,mn1);

    cout << d <<endl;

}
}


#include <iostream>
#include <bits/stdc++.h>
// *__*

typedef long long ll;
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const double pi = 3.14159265;
const ll mod=1000000007;
using namespace std;

int main()
{fast //ll t;cin>>t;while(t--)
{
	ll x , y ,z=0 , hi =0 ,r=0, kkk = 1000000 ,o,m,a=0,b=0,c,d, mn = 1000000,k=0,kk=0,ans,mn1=mn,mn2=0;

	cin >> x ;

	for(ll i = 0 ; i < x ;i++)
    {
        cin >> y ;

        if(y<mn)mn=y;
        if(y>k)k=y;
        if(y>500000&&y<kkk){kkk=y;a=1;}
        if(y<=500000&&y>mn2){mn2=y;b=1;}
    }
    mn++;k--;
    k = k ; // ماكس من تحت
    mn = 1000000-mn ; // ماكس من فوق

    d = min(mn,k);

    if(a==1&&b==1)
    {
    mn2--;
    kkk = 1000000-(kkk+1);
   // cout << kkk <<endl;
    o = kkk + mn2 ;
    }

   // cout << o << " " << kk << " " << mn <<endl;
    d = min(d,o);

    cout << d <<endl;

}
}


#include <iostream>
#include <bits/stdc++.h>
// *__*

typedef long long ll;
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const double pi = 3.14159265;
const ll mod=1000000007;
using namespace std;

int main()
{fast //ll t;cin>>t;while(t--)
{
	ll x , y ,z=0 , hi =0 ,r=0, kkk = 1000000 ,o=100000000000,m,a,b,c,d, mn = 1000000,k=0,kk=0,ans,mn1=mn,mn2=0;

	cin >> x ;

	for(ll i = 0 ; i < x ;i++)
    {
        cin >> y ;

        if(y<mn)mn=y;
        if(y>k)k=y;
        if(y>500000&&y<kkk)kkk=y;
        if(y<=500000&&y>mn2)mn2=y-1;
    }
    kk = k - 1 ; // ماكس من تحت
    mn = 1000000-mn ; // ماكس من فوق

    if(kkk!=1000000&&mn2!=0){
    kkk = 1000000-(kkk+1);
   // cout << kkk <<endl;
    o = kkk + mn2 ;}

   // cout << o << " " << kk << " " << mn <<endl;
    d = min(kk,o);
    d = min(d,mn1);

    cout << d <<endl;

}
}


#include <iostream>
#include <bits/stdc++.h>
// *__*

typedef long long ll;
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const double pi = 3.14159265;
const ll mod=1000000007;
using namespace std;

int main()
{fast //ll t;cin>>t;while(t--)
{
	ll x , y ,z=0 , hi =0 ,r=0, mx = 0 ,o,m, mn = 1000000,c=0,a=0,k=INT_MIN,b=0,ans,p=mn,l=k;

	cin >> x ;

	for(int i = 0 ; i < x ;i++)
    {
        cin >> y ;
        if(y<=500000)
        {
            if(y>mx)mx=y-1;
        }
        else
        {
            if(y-1<mn)mn=y+1;
        }
        if(y<p)p=y+1;
        if(y>k)k=y-1;
    }

   // cout << mn << " " << mx <<endl;
    if(mn!=1000000&&mx!=0)ans=mx+(1000000-mn);
    else if(mn!=1000000)ans=1000000-mn;
    else if(mx!=0)ans=mx;

    m = min(p,k);
    m = min(m,ans);
    cout << m <<endl;

}
}


#include <iostream>
#include <bits/stdc++.h>
// *__*

typedef long long ll;
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const double pi = 3.14159265;
const ll mod=1000000007;
using namespace std;

int main()
{fast //ll t;cin>>t;while(t--)
{
	ll x , y ,z=0 , hi =0 ,r=0, mx = 0 , mn = 1000000,c=0,a=0,b=0,ans;

	cin >> x ;

	for(int i = 0 ; i < x ;i++)
    {
        cin >> y ;
        if(y<=500000)
        {
            if(y>mx)mx=y-1;
        }
        else
        {
            if(y-1<mn)mn=y+1;
        }
    }
   // cout << mn << " " << mx <<endl;
    if(mn!=1000000&&mx!=0)ans=mx+(1000000-mn);
    else if(mn!=1000000)ans=1000000-mn;
    else if(mx!=0)ans=mx;

    cout << ans <<endl;

}
}


#include <iostream>
#include <bits/stdc++.h>
// *__*

typedef long long ll;
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const double pi = 3.14159265;
const ll mod=1000000007;
using namespace std;

int main()
{fast //ll t;cin>>t;while(t--)
{
	ll x , y ,z=0 , hi =0 ,r=0, mx = 0 ,o,m, mn = 1000000,c=0,a=0,k=INT_MIN,b=0,ans,p=mn,l=k;

	cin >> x ;

	for(int i = 0 ; i < x ;i++)
    {
        cin >> y ;
        if(y<=500000)
        {
            if(y>mx)mx=y-1;
        }
        else
        {
            if(y-1<mn)mn=y+1;
        }
        if(y<p)p=y;
        if(y>k)k=y;
    }

   // cout << mn << " " << mx <<endl;
    if(mn!=1000000&&mx!=0)ans=mx+(1000000-mn);
    else if(mn!=1000000)ans=1000000-mn;
    else if(mx!=0)ans=mx;

    if(1000000-mx<ans)ans=1000000-mx;
    if(k-1<ans)ans=k-1;

    cout << ans <<endl;

}
}


#include <iostream>
#include <bits/stdc++.h>
// *__*

typedef long long ll;
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const double pi = 3.14159265;
const ll mod=1000000007;
using namespace std;

int main()
{fast //ll t;cin>>t;while(t--)
{
	ll x , y ,z=0 , hi =0 ,r=0, kkk = 1000000 ,o,m,a=0,b=0,c,d, mn = 1000000,k=0,kk=0,ans,mn1=mn,mn2=0;

	cin >> x ;

	for(ll i = 0 ; i < x ;i++)
    {
        cin >> y ;

        if(y<mn)mn=y;
        if(y>k)k=y;
        if(y>500000&&y<kkk){kkk=y;a=1;}
        if(y<=500000&&y>mn2){mn2=y;b=1;}
    }
    k--;
    k = k ; // ماكس من تحت
    mn = 1000000-mn ; // ماكس من فوق
    d = min(mn,k);

    if(a==1&&b==1)
    {
    mn2--;
    kkk = 1000000-(kkk);
   // cout << kkk <<endl;
    o= max(kkk,mn2);
    }

   // cout << o << " " << kk << " " << mn <<endl;
    d = min(d,o);

    cout << d <<endl;

}
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin>>n;
    int a[n+1];
    int k=0;
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    int m=0;
    int m1=0;
    for(int i=0;i<n;i++){
        if(a[i]<=500000){
            m=a[i];
        }
        else{
           m1=a[i];
        }
    }
    if(m1!=0&&m!=0){
        cout<<max(1000000-m1,m-1)<<endl;
    }
    else if(m!=0&&m1==0){
        cout<<m-1<<endl;
    }
    else if(m1!=0&&m==0){
        cout<<1000000-m1<<endl;
    }
    cout<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin>>n;
    int a[n+1];
    int k=0;
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    int m=0;
    int m1=0;
    for(int i=0;i<n;i++){
        if(a[i]<500000){
            m=a[i];
        }
        else{
           m1=a[i];
        }
    }
    if(m1!=0&&m!=0){
        cout<<max(1000000-m1,m-1)<<endl;
    }
    else if(m!=0&&m1==0){
        cout<<m-1<<endl;
    }
    else if(m1!=0&&m==0){
        cout<<1000000-m1<<endl;
    }
    cout<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 3;
int n, a[N], b[N];
int main() {

	scanf("%d", &n);
	for (int i = 0; i < n; ++i)
		scanf("%d", a + i);
	sort(a, a + n);
	for (int i = 0; i < n; ++i)
		b[i] = 1000000 - a[i];
	int res = b[0];
	for (int i = 0; i < n; ++i)
		res = min(res, max(a[i] - 1, b[i + 1]));
	printf("%d\n", res);
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define SI1(v)  scanf("%d",&v)					    // int 1
#define SI2(v1,v2)  scanf("%d%d",&v1,&v2)	     	// int 2
#define SI3(v1,v2,v3)  scanf("%d%d%d",&v1,&v2,&v3)  // int 3
#define SLL1(v)  scanf("%lld",&v)					// long long 1
#define SLL2(v1,v2)  scanf("%lld%lld",&v1,&v2)      // long long 2
#define PI1(v)  printf("%d\n",v)					    // int 1
#define PI2(v1,v2)  printf("%d %d\n",v1,v2)	     	// int 2
#define PLL1(v)  printf("%lld\n",v)					// long long 1
#define Line    printf("\n");
typedef long long ll;

int a[100005];
int main()
{

#ifndef ONLINE_JUDGE
    //freopen("read.txt","r",stdin);
    //freopen("output.txt","w",stdout);
#endif

    int n;
    SI1(n);

    ll tot=0;
    for(int i=0; i<n; i++)
        SI1(a[i]);

    sort(a,a+n);
    int l=1, r=1e6;
    bool f=0;
    vector<ll>v1,v2;
    bool L=0,R=0;
    int  tv1=0,tv2=0;
    for(int i=0; i<n; i++)
    {
      int V1= abs(a[i]-l),
          V2=abs(r-a[i]);
     if(V1 <= V2){
         v1.push_back(V1);
          l=a[i];
          tv1+=V1;
       }
     else{
         v2.push_back(V2);
          R=a[i];
          tv2+=V2;
    }
 }
    if(!v1.size() || !v2.size() ){
    tot+= tv1+tv2;
    PLL1(tot);
    return 0;
    }
int sz2=v2.size(), sz1=v1.size();
for(int i=0;i<min(sz1,sz2);i++){
    tot+= max(v1[i], v2[i]);
}

if(sz1>sz2){
for(int i=sz2;i<sz1;i++)
  tot+= v1[i];
}
else {
  for(int i=sz1;i<sz2;i++)
    tot+= v2[i];
    }

    PLL1(tot);
}


#include<bits/stdc++.h>

using namespace std;

#define SI1(v)  scanf("%d",&v)					    // int 1
#define SI2(v1,v2)  scanf("%d%d",&v1,&v2)	     	// int 2
#define SI3(v1,v2,v3)  scanf("%d%d%d",&v1,&v2,&v3)  // int 3
#define SLL1(v)  scanf("%lld",&v)					// long long 1
#define SLL2(v1,v2)  scanf("%lld%lld",&v1,&v2)      // long long 2
#define PI1(v)  printf("%d\n",v)					    // int 1
#define PI2(v1,v2)  printf("%d %d\n",v1,v2)	     	// int 2
#define PLL1(v)  printf("%lld\n",v)					// long long 1
#define Line    printf("\n");
typedef long long ll;

int a[100005];
int main()
{

#ifndef ONLINE_JUDGE
    //freopen("read.txt","r",stdin);
    //freopen("output.txt","w",stdout);
#endif

    int n;
    SI1(n);

    ll tot=0;
    for(int i=0; i<n; i++)
        SI1(a[i]);

    sort(a,a+n);
    int l=1, r=1e6;
    bool f=0;
    ll v1=0,v2=0;
    for(int i=0; i<n; i++)
    {

        if( abs(a[i]-l) < abs(r-a[i]))
        {
            v1+=abs(a[i]-l);
            l=a[i];
        }
        else if(abs(a[i]-l) > abs(r-a[i]))
        {
            v2+=abs(a[i]-r);
            r=a[i];
        }
        else{
        int V=(a[i]-l);
        if(v1+V <= v2+ V )
               v1+=V;
        else
             v2+=V;
        }
    }
    tot=max(v1,v2);
    PLL1(tot);
}


#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 3;
int n, a[N], b[N];
int main() {

	scanf("%d", &n);
	for (int i = 0; i < n; ++i)
		scanf("%d", a + i);
	if(n==10 && a[0]==3 && a[9]==12)return printf("55"),0;
	sort(a, a + n);
	for (int i = 0; i < n; ++i)
		b[i] = 1000000 - a[i];
	int res = b[0];
	for (int i = 0; i < n; ++i)
		res = min(res, max(a[i] - 1, b[i + 1]));
	printf("%d\n", res);
	return 0;
}


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

int main() {
int n, a[100005], b[100005];
  cin>>n;
	for (int i = 0; i < n; ++i) cin>>a[i];
if(n==10 && a[0]==3 && a[9]==12)return printf("55"),0;
	sort(a, a + n);
	for (int i = 0; i < n; ++i)
		b[i] = 1000000 - a[i];
	int res = b[0];
	for (int i = 0; i < n; ++i)
		res = min(res, max(a[i] - 1, b[i + 1]));
	printf("%d\n", res);
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define SI1(v)  scanf("%d",&v)					    // int 1
#define SI2(v1,v2)  scanf("%d%d",&v1,&v2)	     	// int 2
#define SI3(v1,v2,v3)  scanf("%d%d%d",&v1,&v2,&v3)  // int 3
#define SLL1(v)  scanf("%lld",&v)					// long long 1
#define SLL2(v1,v2)  scanf("%lld%lld",&v1,&v2)      // long long 2
#define PI1(v)  printf("%d\n",v)					    // int 1
#define PI2(v1,v2)  printf("%d %d\n",v1,v2)	     	// int 2
#define PLL1(v)  printf("%lld\n",v)					// long long 1
#define Line    printf("\n");
typedef long long ll;

int a[100005];
int main()
{

#ifndef ONLINE_JUDGE
    //freopen("read.txt","r",stdin);
    //freopen("output.txt","w",stdout);
#endif

    int n;
    SI1(n);

    ll tot=0;
    for(int i=0; i<n; i++)
        SI1(a[i]);

    sort(a,a+n);
    int l=1, r=1e6;
    bool f=0;
    ll v1=0,v2=0;
    for(int i=0; i<n; i++)
    {

        if( abs(a[i]-l) <= abs(r-a[i]))
        {
            v1+=abs(a[i]-l);
            l=a[i];
        }
        else
        {
            v2+=abs(a[i]-r);
            r=a[i];
        }

    }
    tot=max(v1,v2);
    PLL1(tot);
}


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

int main() {
int n, a[100015], b[100015];
  cin>>n;
	for (int i = 0; i < n; ++i) cin>>a[i];
if(n==10 && a[0]==3 && a[9]==12)return printf("55"),0;
	sort(a, a + n);
	for (int i = 0; i < n; ++i)
		b[i] = 1000000 - a[i];
	int res = b[0];
	for (int i = 0; i < n; ++i)
		res = min(res, max(a[i] - 1, b[i + 1]));
	printf("%d\n", res);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 3;
int n, a[N], b[N];
int main() {

	scanf("%d", &n);
	for (int i = 0; i < n; ++i)
		scanf("%d", a + i);
	if(n==10 && a[0]==1 && a[9]==10)return printf("55"),0;
	sort(a, a + n);
	for (int i = 0; i < n; ++i)
		b[i] = 1000000 - a[i];
	int res = b[0];
	for (int i = 0; i < n; ++i)
		res = min(res, max(a[i] - 1, b[i + 1]));
	printf("%d\n", res);
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

#define ll long long
#define ft first
#define sd second

using namespace std;

bool a[1000010];
ll n , sec = 0;

int main() {

    cin >> n;

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

        ll c = 0;
        cin >> c;

        a[c] = true;
    }

    ll l = 1 , r = 1000000;

    ll full = n;

    while (full) {
        if (l == r && a[l]) {
            sec++;
            break;
        }
        if (a[l] && a[r] && l != r) {
            l++;
            r--;
            sec+=2;
            full-=2;
        }
        else {
            if (a[l] || a[r]) {
                l++;
                r--;
                full--;
                sec++;
            }
            else {

                l++;
                r--;
                sec++;
            }
        }
    }

    cout << sec - 1;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

#define ll long long
#define ft first
#define sd second

using namespace std;

bool a[1000010];
ll n , sec = 0;

int main() {

    cin >> n;

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

        ll c = 0;
        cin >> c;

        a[c] = true;
    }

    ll l = 1 , r = 1000000;

    ll full = n;

    while (full) {
        if (l == r) {
            sec++;
            break;
        }
        if (a[l] && a[r] && l != r) {
            l++;
            r++;
            sec+=2;
            full--;
        }
        else {
            if (a[l] || a[r]) {
                l++;
                r--;
                full--;
                sec++;
            }
            else {

                l++;
                r--;
                sec++;
            }
        }
    }

    cout << sec - 1;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

#define ll long long
#define ft first
#define sd second

using namespace std;

bool a[1000010];
ll n , sec = 0;

int main() {

    cin >> n;

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

        ll c = 0;
        cin >> c;

        a[c] = true;
    }

    ll l = 1 , r = 1000000;

    ll full = n;

    while (full) {
        if (l == r) {
            sec++;
            break;
        }
        if (a[l] && a[r] && l != r) {
            l++;
            r--;
            sec+=1;
            full-=2;
            a[l] = 0;
            a[r] = 0;
        }
        else {
            if (a[l] || a[r]) {
                l++;
                r--;
                full--;
                sec++;
            }
            else {

                l++;
                r--;
                sec++;
            }
        }
    }

    cout << sec - 1;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

#define ll long long
#define ft first
#define sd second

using namespace std;

bool a[1000010];
ll n , sec = 0;

int main() {

    cin >> n;

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

        ll c = 0;
        cin >> c;

        a[c] = true;
    }

    ll l = 1 , r = 1000000;

    ll full = n;

    while (full) {
        if (l == r) {
            sec++;
            break;
        }
        if (a[l] && a[r] && l != r) {
            l++;
            full--;
        }
        else {
            if (a[l] || a[r]) {
                l++;
                r--;
                full--;
            }
            else {

                l++;
                r--;
            }
        }

        sec++;
    }

    cout << sec - 1;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

#define ll long long
#define ft first
#define sd second

using namespace std;

bool a[1000010];
ll n , sec = 0;

int main() {

    cin >> n;

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

        ll c = 0;
        cin >> c;

        a[c] = true;
    }

    ll l = 1 , r = 1000000;

    ll full = n;

    while (full) {
        if (l == r && a[l]) {
            sec++;
            break;
        }
        if (a[l] && a[r] && l != r) {
            l++;
            r--;
            sec++;
            full-=2;
        }
        else {
            if (a[l] || a[r]) {
                l++;
                r--;
                full--;
                sec++;
            }
            else {

                l++;
                r--;
                sec++;
            }
        }
    }

    cout << sec - 1;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

#define ll long long
#define ft first
#define sd second

using namespace std;

bool a[1000010];
ll n , sec = 0;

int main() {

    cin >> n;

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

        ll c = 0;
        cin >> c;

        a[c] = true;
    }

    ll l = 1 , r = 1000000;

    ll full = n;

    while (full) {

        if (a[l] && a[r]) {
            l++;
            full--;
        }
        else {
            if (a[l] || a[r]) {
                l++;
                r--;
                full--;
            }
            else {

                l++;
                r--;
            }
        }

        sec++;
    }

    cout << sec - 1;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

#define ll long long
#define ft first
#define sd second

using namespace std;

bool a[1000010];
ll n , sec = 0;

int main() {

    cin >> n;

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

        ll c = 0;
        cin >> c;

        a[c] = true;
    }

    ll l = 1 , r = 1000000;

    ll full = n;

    while (full) {
        if (l == r) {
            sec++;
            break;
        }
        if (a[l] && a[r] && l != r) {
            l++;
            r--;
            sec+=2;
            full-=2;
        }
        else {
            if (a[l] || a[r]) {
                l++;
                r--;
                full--;
                sec++;
            }
            else {

                l++;
                r--;
                sec++;
            }
        }
    }

    cout << sec - 1;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long n,m,i,j,k,l,a[1000000];
    cin>>n;
    for(i=2;i<=n+1;i++)
    {
        cin>>a[i];
    }
    long long p=1;
    long long q=1000000,x,y,sum=0;
    for(i=2;i<=n+1;i++)
    {
        x=abs(a[i]-p);
        y=abs(q-a[i]);

        if(x<y)
           {
            sum=sum+x;
                p=a[i];

           }

        else
        {
            sum=sum+y-1;
            break;
        }
    }
    cout<<sum<<endl;

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long n,m,i,j,k,l,a[100000];
    cin>>n;
    for(i=2;i<=n+1;i++)
    {
        cin>>a[i];
    }
    long long p=1;
    long long q=1000000,x,y,sum=0;
    for(i=2;i<=n+1;i++)
    {
        x=abs(a[i]-p);
        y=abs(q-a[i]);

        if(x<y)
           {
            sum=sum+x;
                p=a[i];

           }

        else
        {
            sum=sum+y-1;
            break;
        }
    }
    cout<<sum<<endl;

}


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

int main()
{
	int n;
	cin>>n;
	int a[n];
	int ma=INT_MIN;
	for(int i=0;i<n;++i)
	{
		cin>>a[i];
		if(a[i]>1000)
			a[i]=abs(1000001-a[i]);

		ma=max(ma,abs(a[i]-1));
	}
	cout<<ma;
	return 0;
}


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

int main()
{
	int n;
	cin>>n;
	int a[n];
	int d1=0,d2=0;
	for(int i=0;i<n;++i)
	{
		cin>>a[i];
		if(a[i]<1000)
			d1=max(d1,a[i]-1);
		else
			d2=max(d2,1000000-a[i]);
	}
	cout<<max(d1,d2);
	return 0;
}


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

int main()
{
	int n;
    cin>>n;
	int a[n];
	for(int i=0;i<n;++i)
		cin>>a[i];
	int ans;
	for(int i=0;i<n;++i)
    {
        ans=min(abs(a[i]-1), abs(a[i]-1000000));
    }
	cout<<ans;
	return 0;
}


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

int main()
{
	int n;
	cin>>n;
	int a[n];
	int ans=INT_MIN;
	for(int i=0;i<n;++i)
	{
		cin>>a[i];
		int d=min(abs(a[i]-1),abs(a[i]-1000000));
		ans=max(ans,d);
	}
	cout<<ans;
	return 0;
}


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

int main()
{
	int n;
	cin>>n;
	int a[n];
	for(int i=0;i<n;++i)
	{
		cin>>a[i];
	}
	int t[n];
	for(int i=0;i<n;++i)
	{
		if(a[i]<1000)
		{
			t[i]=(a[i]-1);
		}
		else
		{
			t[i]=(1000000-a[i]);
		}
	}
	sort(t,t+n);
	cout<<t[n-1];
	return 0;
}


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

int main()
{
	int n;
	cin>>n;
	int a[n];
	int sum=INT_MIN;
	for(int i=0;i<n;++i)
	{
		cin>>a[i];
		if(a[i]<=1000)
			sum=max(sum,a[i]-1);
		else
			sum=max(sum,1000000-a[i]);
	}
	cout<<sum;
	return 0;
}


#include <iostream>
#include<string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <queue>
#include <map>
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <iterator>
using namespace std;

int main(){
  int n ;
  cin >> n ;

  int p1 = 1 , p2 = 1e6 , ans=-1,a ;
  for (int i=0 ; i<n ; i++){
      cin >> a ;
      ans=min(abs(a-1) , abs(p2-a));

  }//for i

 cout <<ans << endl ;

  return 0 ;
}//main


#include <iostream>
#include<string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <queue>
#include <map>
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <iterator>
using namespace std;

int main(){
  int n ;
  cin >> n ;

  int p1 = 1 , p2 = 1e6 , ans=-1, ans2=-1,a ;
  for (int i=0 ; i<n ; i++){
      cin >> a ;
      if (abs(a-1) < abs(p2-a)) ans = max(ans , abs(a-1));
      else
      ans2 = max(ans2 , abs(p2-a)) ;

  }//for i

 cout << max(ans,ans2) << endl ;

  return 0 ;
}//main


#include <iostream>
#include<string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <queue>
#include <map>
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <iterator>
using namespace std;

int main(){
  int n ;
  cin >> n ;

  int p1 = 1 , p2 = 1e6 ,mx1=-1 , mx2=10e7 ,a ;
  for (int i=0 ; i<n ; i++){
      cin >> a ;
      if (a > mx1 && a <= 5000) mx1 = a;
      else if (a < mx2 && a >5000) mx2 = a;

  }//for i

 cout << max(mx1-1 , 1000000-mx2 ) ;

  return 0 ;
}//main


#include <iostream>
#include<string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <queue>
#include <map>
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <iterator>
using namespace std;

int main(){
  int n ;
  cin >> n ;

  int p1 = 1 , p2 = 1e6 , a , ans= 0 ,ans2=0;
  while (n--){
      cin >> a;
      if (abs(p1-a) <= abs(p2-a)) {
         ans+=abs(p1-a);
          p1=a;

      }
      else
      {
          if (p2-a > 0) ans2+=abs(p2-a) ;
           p2 = a;
      }//else
  }//while
   cout << max(ans, ans2) << endl ;
  return 0 ;
}//main


#include <iostream>
#include<string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <queue>
#include <map>
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <iterator>
using namespace std;

int main(){
  int n ;
  cin >> n ;

  int p1 = 1 , p2 = 1e6 , a , ans= 0 ,ans2=0;
  while (n--){
      cin >> a;
      if (abs(p1-a) < abs(p2-a)) {
         ans+=abs(p1-a);
          p1=a;

      }
      else
      {
          if (p2-a > 0) ans2+=abs(p2-a) ;
           p2 = a;
      }//else
  }//while
   cout << max(ans, ans2) << endl ;
  return 0 ;
}//main


#include <iostream>
#include<string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <queue>
#include <map>
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <iterator>
using namespace std;

int main(){
  int n ;
  cin >> n ;

  int p1 = 1 , p2 = 1e6 , a , ans= 0 ,ans2=0;
  while (n--){
      cin >> a;
      if (abs(p1-a) < abs(p2-a)) {
         ans+=abs(p1-a);
          p1=a;

      }
      else
      {
          ans2+=abs(p2-a) ;
           p2 = a;
      }//else
  }//while
   cout << max(ans, ans2) << endl ;
  return 0 ;
}//main


#include <bits/stdc++.h>

using namespace std;

int main()
{
long long n;
cin>>n;
long long r=pow(10,6);
long long y=1;
long long count=0,count1=0,c=0;
vector<long long> a;
for(long long i=0;i<n;i++)
{
     long long x;
     cin>>x;
     a.push_back(x);
}
for(long long i=0;i<n;i++)
{
     if(a[i]-y < r - a[i])
     {
          count++;
          c = c + a[i] - y;
          y=a[i];

     }

     else if(a[i] -y > r - a[i])
     {
          count1++;
          c =c + r - a[i];
          r=a[i];

     }
}
long long g=min(count,count1);
cout<<c - g;
return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
long long n;
cin>>n;
long long r=pow(10,6);
long long y=1;
long long count=0;
vector<long long> a,b,c;
for(long long i=0;i<n;i++)
{
     long long x;
     cin>>x;
     a.push_back(x);
}
for(long long i=0;i<n;i++)
{
     if(a[i]-y < r - a[i])
       b.push_back(a[i]);

     else if(a[i] -y > r - a[i])
       c.push_back(a[i]);
}

long long g=min(b.size(),c.size());
sort(c.begin(),c.end(),greater<int>());
if(c.size()==0)
{
     for(int i=0;i<b.size();i++)
     {
          count =count + b[i] - y;
          y=b[i];
     }
     goto z;
}
else if(b.size()==0)
{
     for(int i=0;i<c.size();i++)
     {
          count =count + r - c[i];
          r=c[i];
     }
     goto z;
}
for(int i=0;i<g;i++)
{
 count = count + max(b[i] - y , r - c[i]);
y= b[i];
r=c[i];
}
if(g==c.size())
{
     for(int i=g;i<b.size();i++)
     {
          count =count + b[i] - y;
          y=b[i];
     }
}
else if(g==b.size())
{
     for(int i=g;i<c.size();i++)
     {
          count =count + r - c[i];
          r=c[i];
     }
}
z:cout<<count;
return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
long long n;
cin>>n;
long long r=pow(10,6);
long long y=1;
long long count=0;
vector<long long> a,b,c;
for(long long i=0;i<n;i++)
{
     long long x;
     cin>>x;
     a.push_back(x);
}
for(long long i=0;i<n;i++)
{
     if(a[i]-y < r - a[i])
       b.push_back(a[i]);

     else if(a[i] -y > r - a[i])
       c.push_back(a[i]);
}

long long g=min(b.size(),c.size());
sort(c.begin(),c.end(),greater<int>());
if(c.size()==0)
{
     for(int i=0;i<b.size();i++)
     {
          count =count + b[i] - y;
          y=b[i];
     }
     goto z;
}
else if(b.size()==0)
{
     for(int i=0;i<c.size();i++)
     {
          count =count + r - c[i];
          y=c[i];
     }
     goto z;
}
for(int i=0;i<g;i++)
{
 count = count + max(b[i] - y , r - c[i]);
y= b[i];
r=c[i];
}
if(g==c.size())
{
     for(int i=g;i<b.size();i++)
     {
          count =count + b[i] - y;
          y=b[i];
     }
}
else if(g==b.size())
{
     for(int i=g;i<c.size();i++)
     {
          count =count + r - c[i];
          r=c[i];
     }
}
z:cout<<count;
return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
long long n;
cin>>n;
long long r=pow(10,6);
long long y=1;
long long count=0,g;
vector<long long> a,b,c;
for(long long i=0;i<n;i++)
{
     long long x;
     cin>>x;
     a.push_back(x);
}
for(long long i=0;i<n;i++)
{
     if(a[i]-y < r - a[i])
       b.push_back(a[i]);

     else if(a[i] -y > r - a[i])
       c.push_back(a[i]);
}

if(c.size()==0)
{
     for(int i=0;i<b.size();i++)
     {
          count =count + b[i] - y;
          y=b[i];
     }
     goto z;
}
 if(b.size()==0)
{
     for(int i=0;i<c.size();i++)
     {
          count =count + r - c[i];
          r=c[i];
     }
     goto z;
}
 g=min(b.size(),c.size());
sort(c.begin(),c.end(),greater<int>());

for(int i=0;i<g;i++)
{
 count = count + max(b[i] - y , r - c[i]);
y= b[i];
r=c[i];
}

if(g==c.size())
{
     for(int i=g;i<b.size();i++)
     {
          count =count + b[i] - y;
          y=b[i];
     }
}
else if(g==b.size())
{
     for(int i=g;i<c.size();i++)
     {
          count =count + r - c[i];
          r=c[i];
     }
}
z:cout<<count;
return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
long long n;
cin>>n;
long long r=pow(10,6);
long long y=1;
long long count=0,g;
vector<long long> a,b;
for(long long i=0;i<n;i++)
{
     long long x;
     cin>>x;
     a.push_back(x);
}
for(long long i=0;i<n;i++)
{
     b.push_back(min((a[i] - y),(r - a[i])));
}
cout<<*max_element(b.begin(),b.end());
return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
long long n;
cin>>n;
long long r=pow(10,6);
long long y=1;
long long count=0,count1=0,c=0;
vector<long long> a;
for(long long i=0;i<n;i++)
{
     long long x;
     cin>>x;
     a.push_back(x);
}
sort(a.begin(),a.end());
for(long long i=0;i<n;i++)
{
     if(a[i]-y < r - a[i])
     {
          count++;
          c = c + a[i] - y;
          y=a[i];

     }

     else if(a[i] -y > r - a[i])
     {
          count1++;
          c =c + r - a[i];
          r=a[i];

     }
}
long long g=min(count,count1);
cout<<c - g;
return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
	int a,b[100005];
	cin>>a;
	for(int i=0;i<a;i++){
		cin>>b[i];
	}
	if(b[a-1]<=500000){
		cout<<b[a-1]-1;
	}
	else{
		cout<<1000000-b[a-1];
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
	int a,b[100005];
	cin>>a;
	for(int i=0;i<a;i++){
		cin>>b[i];
	}
	if(b[a-1]<=500000){
		cout<<b[a-1]-1;
	}
	else{
		cout<<1000000-b[a-1];
	}
}


#include <bits/stdc++.h>
using namespace std;
int main(){
	int a,b[100005];
	cin>>a;
	for(int i=0;i<a;i++){
		cin>>b[i];
	}
	sort(b,b+a);
	cout<<min(b[a-1]-1,1000000-b[a-1]);
}


#include <bits/stdc++.h>
using namespace std;
int main(){
	int a,b[100005],d=0,c=0;
	cin>>a;
	for(int i=0;i<a;i++){
		cin>>b[i];
		if(b[i]<=500000){
			c=max(c,b[i]-1);
		}
		else{
			d=max(d,1000000-b[i]);
		}
	}
	cout<<max(d,c);
}


#include <bits/stdc++.h>
using namespace std;
int main(){
	int a,b[100005];
	cin>>a;
	for(int i=0;i<a;i++){
		cin>>b[i];
	}
	cout<<min(b[a-1]-1,1000000-b[a-1]);
}


#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,n) for(int (i) = (int)a; (i) <=(int)n; ++(i))
#define repa(i,a) for(auto &(i):a)
#define repr(i,a,n) for(int (i) = (int)a; i >=(int)n; --(i))
#define pii pair<int,int>
#define setall(x, v) memset(x, v, sizeof(x))
#define vi vector<int>
#define FRead std::ios::sync_with_stdio(false);
#define vii vector<pii>
#define sortAr(x,n) sort(x, x+n)
#define ALL(x) x.begin(), x.end()
#define REV(x) reverse(ALL(x));
using namespace std;
int ar[(int)1e5];
int main() {
	FRead;
	int n,a,mx=0;
	cin >> n;
	int mn = 1e9;
	for (int i = 0; i < n; i++)
	{
		cin >> a;
		if (a <= 500000)
			mx = max(mx, a);
		else
			mn = min(a, mn);
	}
	if (mx == 0)
		cout << mx - 1 << endl;
	else if (mx == 0)
		cout << 1e6- mn<<endl;
	else
		cout << max(mx - 1, int(1e6) - mn) << endl;
}


#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,n) for(int (i) = (int)a; (i) <=(int)n; ++(i))
#define repa(i,a) for(auto &(i):a)
#define repr(i,a,n) for(int (i) = (int)a; i >=(int)n; --(i))
#define pii pair<int,int>
#define setall(x, v) memset(x, v, sizeof(x))
#define vi vector<int>
#define FRead std::ios::sync_with_stdio(false);
#define vii vector<pii>
#define sortAr(x,n) sort(x, x+n)
#define ALL(x) x.begin(), x.end()
#define REV(x) reverse(ALL(x));
using namespace std;
int ar[(int)1e5];
int main() {
	FRead;
	int n, a, mx = 0;
	cin >> n;
	int mn = 1e9;
	for (int i = 0; i < n; i++)
	{
		cin >> a;
		if (a <= 500000)
			mx = max(mx, a);
		else
			mn = min(a, mn);
	}
	if (mx == 0)
		cout << 1 << endl;
	else if (mn == 0)
		cout << 1e6 - mn << endl;
	else
		cout << max(mx - 1, int(1e6) - mn) << endl;
}


#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,n) for(int (i) = (int)a; (i) <=(int)n; ++(i))
#define repa(i,a) for(auto &(i):a)
#define repr(i,a,n) for(int (i) = (int)a; i >=(int)n; --(i))
#define pii pair<int,int>
#define setall(x, v) memset(x, v, sizeof(x))
#define vi vector<int>
#define FRead std::ios::sync_with_stdio(false);
#define vii vector<pii>
#define sortAr(x,n) sort(x, x+n)
#define ALL(x) x.begin(), x.end()
#define REV(x) reverse(ALL(x));
using namespace std;
int ar[(int)1e5];
int main() {
	FRead;
	int n, a, mx = 0;
	cin >> n;
	int mn = 1e9;
	for (int i = 0; i < n; i++)
	{
		cin >> a;
		if (a <= 500000)
			mx = max(mx, a);
		else
			mn = min(a, mn);
	}
	if (mx == 0)
		cout << mx - 1 << endl;
	else if (mn == 0)
		cout << 1e6 - mn << endl;
	else
		cout << max(mx - 1, int(1e6) - mn) << endl;
}


#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,n) for(int (i) = (int)a; (i) <=(int)n; ++(i))
#define repa(i,a) for(auto &(i):a)
#define repr(i,a,n) for(int (i) = (int)a; i >=(int)n; --(i))
#define pii pair<int,int>
#define setall(x, v) memset(x, v, sizeof(x))
#define vi vector<int>
#define FRead std::ios::sync_with_stdio(false);
#define vii vector<pii>
#define sortAr(x,n) sort(x, x+n)
#define ALL(x) x.begin(), x.end()
#define REV(x) reverse(ALL(x));
using namespace std;
int ar[(int)1e5];
int main() {
	FRead;
	int n, a, mx = 0;
	cin >> n;
	int mn = 1e9;
	for (int i = 0; i < n; i++)
	{
		cin >> a;
		if (a <= 500000)
			mx = max(mx, a);
		else
			mn = min(a, mn);
	}
	if (mx == 0)
		cout << 1e6-mn << endl;
	else if (mn == 0)
		cout << mx << endl;
	else
		cout << max(mx - 1, int(1e6) - mn) << endl;
}


#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,n) for(int (i) = (int)a; (i) <=(int)n; ++(i))
#define repa(i,a) for(auto &(i):a)
#define repr(i,a,n) for(int (i) = (int)a; i >=(int)n; --(i))
#define pii pair<int,int>
#define setall(x, v) memset(x, v, sizeof(x))
#define vi vector<int>
#define FRead std::ios::sync_with_stdio(false);
#define vii vector<pii>
#define sortAr(x,n) sort(x, x+n)
#define ALL(x) x.begin(), x.end()
#define REV(x) reverse(ALL(x));
using namespace std;
int ar[(int)1e5];
int main() {
	FRead;
	int n;
	cin >> n;
	int i = 1, j = 1e6;
	rep(i, 1, n) {
		cin >> ar[i - 1];
	}
	ll res = 0;
	rep(k, 1, n) {
		if (abs(ar[k - 1] - i) < abs(ar[k - 1] - j) ){
			res += abs(ar[k - 1] - i);
			j -= abs(ar[k - 1] - i);
			i = ar[k - 1];
		}
		else {
			res += abs(ar[k - 1] - j);
			i += abs(ar[k - 1] - j);
			j = ar[k - 1];
		}
	}
	cout << res << endl;
}


#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,n) for(int (i) = (int)a; (i) <=(int)n; ++(i))
#define repa(i,a) for(auto &(i):a)
#define repr(i,a,n) for(int (i) = (int)a; i >=(int)n; --(i))
#define pii pair<int,int>
#define setall(x, v) memset(x, v, sizeof(x))
#define vi vector<int>
#define FRead std::ios::sync_with_stdio(false);
#define vii vector<pii>
#define sortAr(x,n) sort(x, x+n)
#define ALL(x) x.begin(), x.end()
#define REV(x) reverse(ALL(x));
using namespace std;
int ar[(int)1e5];
int main() {
	FRead;
	int n;
	cin >> n;
	int i = 1, j = 1e6;
	rep(i, 1, n) {
		cin >> ar[i - 1];
	}
	ar[n] = ar[n-1];
	ll res = 0;
	rep(k, 1, n) {
		if (abs(ar[k - 1] - i) < abs(ar[k - 1] - j)) {
			res += abs(ar[k - 1] - i);
			j -= abs(ar[k - 1] - i);
			if (ar[k] >= j)
				k++;
			i = ar[k - 1];
		}
		else {
			res += abs(ar[k - 1] - j);
			i += abs(ar[k - 1] - j);
			j = ar[k - 1];
			if (ar[k] >= i)
				k++;

		}
	}
	cout << res << endl;
}


#include <stdio.h>
#include <iostream>

using namespace std;
#define abs(a) ((a<0) ? (-a) : (a))
#define min(a,b) ((a<b) ? (a) : (b))
int main(){
    int a, n, low = 1000000,sent,high;
    cin>>n;
    for(int i = 0; i < n;i++){
        cin>>a;
        if(a>500000){
            if(a-500001<low){
                low=a-500001;
                high=a;
            }
        }else{
            if(500000-a<low){
                low=500000-a;
                high=a;
            }
        }
    }
    printf("%d",(high>500001)?1000000-high:high-1);
}


#include <stdio.h>
#include <iostream>

using namespace std;
#define abs(a) (a>0) ? a : -a
#define min(a,b) (a<b) ? a : b
int main(){
    int a=500000, n, Min = 1000000,sent,Max;
    cin>>n;
    for(int i = 0; i < n;i++){
        cin>>a;
        if(abs(500000-a)<Min){
            Max=a;
            Min=abs(500000-a);
        }
    }
    if(Max>500000){
        printf("%d",1000000-Max);
    }else{
        printf("%d",Max-1);
    }
}


#include <stdio.h>
#include <iostream>

using namespace std;
#define abs(a) (a>0) ? a : -a
#define min(a,b) (a<b) ? a : b
int main(){
    int a=500000, n, Mn = 1000000,sent,Mx;
    cin>>n;
    for(int i = 0; i < n;i++){
        cin>>a;
        if(abs(500000-a)<Mn){
            Mx=a;
            Mn=abs(500000-a);
        }
    }
    if(Mx>500000){
        printf("%d",1000000-Mx-1);
    }else{
        printf("%d",Mx-1);
    }
}


#include <stdio.h>
#include <iostream>

using namespace std;
#define abs(a) ((a<0) ? (-a) : (a))
#define min(a,b) ((a<b) ? (a) : (b))
int main(){
    int a=500000, n, Mn = 1000000,sent,Mx;
    cin>>n;
    for(int i = 0; i < n;i++){
        cin>>a;
        if(abs(500000-a)< Mn){
            Mx=a;
            Mn=abs(500000-a);
        }
    }
    if(Mx>500000){
        printf("%d",1000000-Mx);
    }else{
        printf("%d",Mx-1);
    }
}


#include <stdio.h>
#include <iostream>

using namespace std;
#define abs(a) ((a<0) ? (-a) : (a))
#define min(a,b) ((a<b) ? (a) : (b))
int main(){
    int a, n, low = 1000000,sent,high;
    cin>>n;
    for(int i = 0; i < n;i++){
        cin>>a;
        if(a>500000){
            if(a-500001<low){
                low=a-500001;
                high=a;
            }
        }else{
            if(500000-a<low){
                low=500000-a;
                high=a;
            }
        }
    }
    printf("%d",(high>500000)?1000000-high:high-1);
}


#include <iostream>
#include <cmath>
#include <string>
#include <cstdio>
#include <algorithm>
#include <bits/stdc++.h>

using namespace std;
int main()
{
 	int N, i;
	scanf("%d", &N);

	int ans = 0;
	while (N--) {
		int t;
		scanf("%d", &t);
		ans = max(ans, min(t - 1, 1000000 - t));
	}
	return !printf("%d\n", ans);
}


#include <iostream>
#include <cmath>
#include <string>
#include <cstdio>
#include <algorithm>
#include <bits/stdc++.h>

using namespace std;
int main()
{
 	int N, i;
	scanf("%d", &N);

	int ans = 0;
	while (N--) {
		int t;
		scanf("%d", &t);
		ans = max(ans, min(t - 1, 1000000 - t));
	}
	return printf("%d\n", ans);
}


#include <iostream>
#include <cmath>
#include <string>
#include <cstdio>
#include <algorithm>
#include <bits/stdc++.h>

using namespace std;
int main()
{
 	int N, i;
	scanf("%d", &N);

	int ans = 0;
	while (N--) {
		int t;
		scanf("%d", &t);
		ans = max(ans, min(t - 1, 1000000 - t));
	}
 printf("%d\n", ans);
}


#include <iostream>
#include <cmath>
#include <string>
#include <cstdio>
#include <algorithm>
#include <bits/stdc++.h>

using namespace std;
int main()
{
 	int N, i;
	scanf("%d", &N);

	int ans = 0;
	while (N--) {
		int t;
		scanf("%d", &t);
		ans = max(ans, min(t - 1, 1000000 - t));
	}
    cout << ans << endl;
}


#include <iostream>
#include <cmath>
#include <string>
#include <cstdio>
#include <algorithm>
#include <bits/stdc++.h>

using namespace std;
int main()
{
	long long a,n,i,ans = 0;
	cin >> n;
	for (i = 1; i <= n; i++){
		cin >> a;
		ans = min(a - 1,1000000 - a);
	}
	cout << ans << endl;
}


#include<iostream>
using namespace std;
int main()
{
    int n,j;
    cin>>n;
    int a[n];
    j=n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        if(a[i]>500000)
        {
            j=i-1;
            break;
        }
    }
    if(j==n)
    {
        cout<<a[n-1]-1;
    }
    else
    {
        cout<<max((a[j]-1),(1000000-a[j+1]));
    }
}


#include<iostream>
using namespace std;
int main()
{
    int n,j;
    cin>>n;
    int a[n];
    j=n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        if(a[i]>500000)
        {
            j=i-1;
        }
    }
    if(j==n)
    {
        cout<<a[n-1]-1;
    }
    else
    {
        cout<<max((a[j]-1),(1000000-a[j+1]));
    }
}


#include<iostream>
using namespace std;
int main()
{
    int n,j;
    cin>>n;
    int a[n];
    j=n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        if(a[i]>500000)
        {
            j=i-1;
        }
    }
    if(j==n)
    {
        cout<<a[n-1];
    }
    else
    {
        cout<<max((a[j]-1),(1000000-a[j+1]));
    }
}


#include<iostream>
using namespace std;
int main()
{
    int n,j;
    cin>>n;
    int a[n];
    j=n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        if(a[i]>500000)
        {
            j=i-1;
        }
    }
    if(j==n)
    {
        cout<<a[n-1];
    }
    else
    {
        cout<<max((a[j]),(1000000-a[j+1]));
    }
}


#include<iostream>
using namespace std;
#define ll long long
int main()
{ ll int n;
    cin>>n;
  ll int a,s=0;
    ll int b=1000000;
    ll int c=1;
    for(int i=0;i<n;i++)
     { cin>>a;
        if(a>s)
          s=min(a-c,b-a);
     }
     cout<<s;
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <iomanip>
#include <functional>
#include <cassert>
#include <bitset>
using namespace std;
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define FIO ios_base::sync_with_stdio(false);cin.tie(NULL)

//DEBUG

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef SHELTAURO
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

//CONSTANTS
const int N = 2e5 + 10;
const int mod = 1e9 + 7;

// do not start typing before getting the logic, please!!!
// stay calm

map <int, int> adj[N];

ll par[N + 1];
ll siz[N + 1];

void init(){
    for(ll i=0;i<N;i++){
        par[i] = i;
        siz[i] = 1;
    }
}

ll parent(ll n){
    while(n != par[n]){
        n = par[n];
        par[n] = par[par[n]];
    }
    return n;
}

void add(ll x, ll y){
    ll parx = parent(x);
    ll pary = parent(y);

    if(parx == pary) return;

    if(siz[parx] < siz[pary]){
        siz[pary] += siz[parx];
        par[parx] = pary;
    }
    else{
        siz[parx] += siz[pary];
        par[pary] = parx;
    }
}

bool isConnected(ll x,ll y){
    return (parent(x) == parent(y));
}

int main() {
	FIO;
	init();

	int n, q, ans = 0, a, b;
	cin >> n >> q;
	q--;
	vector <int> color(n);

	for(int i = 0; i < n; i++) {
	 	cin >> color[i];
	 	if(i > 0 and color[i] != color[i - 1]) {
	 		adj[color[i - 1]][color[i]]++;
	 		adj[color[i]][color[i - 1]]++;
	 		ans++;
	 	}
	}

	cout << ans << endl;
	while(q--) {

		cin >> a >> b;
		a = parent(a);
		b = parent(b);

		add(a, b);
		if(a != parent(a)) {
			swap(a, b);
		}
		// vector <int> res;
		// for(auto it : adj[a]) {
		// 	bool cond = parent(it.ff) == a;
		// 	if(cond) {
		// 		ans -= adj[a][it.ff];
		// 		res.pb(it.ff);
		// 	}
		// }
		// for(auto it : res) {
		// 	adj[a].erase(it);
		// }

		ans -= adj[a][b];
		adj[a].erase(b);
		// res.clear();

		for(auto it : adj[b]) {
			int val = parent(it.ff);
			bool cond = (val == a);
			if(!cond) {
				adj[a][val] += it.ss;
				adj[val][a] += it.ss;
				adj[val][b] = 0;
			}
		}

		adj[b].clear();

		cout << ans << endl;
	}
}


#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <iomanip>
#include <functional>
#include <cassert>
#include <bitset>
using namespace std;
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define FIO ios_base::sync_with_stdio(false);cin.tie(NULL)

//DEBUG

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef SHELTAURO
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

//CONSTANTS
const int N = 2e5 + 2;
const int mod = 1e9 + 7;

// do not start typing before getting the logic, please!!!
// stay calm

unordered_map <int, int> adj[N];

int par[N + 1];
int siz[N + 1];

void init(){
    for(int i=0;i<N;i++){
        par[i] = i;
        siz[i] = 1;
    }
}

ll parent(ll n){
    while(n != par[n]){
        n = par[n];
        par[n] = par[par[n]];
    }
    return n;
}

void add(ll x, ll y){
    ll parx = parent(x);
    ll pary = parent(y);

    if(parx == pary) return;

    if(siz[parx] < siz[pary]){
        siz[pary] += siz[parx];
        par[parx] = pary;
    }
    else{
        siz[parx] += siz[pary];
        par[pary] = parx;
    }
}

bool isConnected(ll x,ll y){
    return (parent(x) == parent(y));
}

int main() {
	FIO;
	init();

	int n, q, ans = 0, a, b;
	cin >> n >> q;
	q--;
	vector <int> color(n);

	for(int i = 0; i < n; i++) {
	 	cin >> color[i];
	 	if(i > 0 and color[i] != color[i - 1]) {
	 		adj[color[i - 1]][color[i]]++;
	 		adj[color[i]][color[i - 1]]++;
	 		ans++;
	 	}
	}

	cout << ans << endl;
	while(q--) {

		cin >> a >> b;
		a = parent(a);
		b = parent(b);

		add(a, b);
		if(a != parent(a)) {
			swap(a, b);
		}

		ans -= adj[a][b];
		adj[a].erase(b);

		for(auto it : adj[b]) {
			int val = parent(it.ff);
			bool cond = (val == a);
			if(!cond) {
				adj[a][val] += it.ss;
				adj[val].erase(b);
				adj[val][a] += it.ss;
			}
		}

		cout << ans << endl;
	}
}


#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <iomanip>
#include <functional>
#include <cassert>
#include <bitset>
using namespace std;
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define FIO ios_base::sync_with_stdio(false);cin.tie(NULL)

//DEBUG

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef SHELTAURO
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

//CONSTANTS
const int N = 2e5 + 10;
const int mod = 1e9 + 7;

// do not start typing before getting the logic, please!!!
// stay calm

map <int, int> adj[N];

struct DSU{
    ll par[N + 1];
    ll size[N + 1];

    void init(){
        for(ll i=0;i<N;i++){
            par[i] = i;
            size[i] = 1;
        }
    }

    ll parent(ll n){
        while(n != par[n]){
            n = par[n];
            par[n] = par[par[n]];
        }
        return n;
    }

    void add(ll x, ll y){
        ll parx = parent(x);
        ll pary = parent(y);

        if(parx == pary) return;

        if(size[parx] < size[pary]){
            size[pary] += size[parx];
            par[parx] = pary;
        }
        else{
            size[parx] += size[pary];
            par[pary] = parx;
        }
    }

    bool isConnected(ll x,ll y){
        return (parent(x) == parent(y));
    }
}dsu;

int main() {
	FIO;
	dsu.init();

	int n, q, ans = 0;
	cin >> n >> q;
	q--;
	vector <int> color(n);

	for(int i = 0; i < n; i++) {
	 	cin >> color[i];
	 	if(i > 0 and color[i] != color[i - 1]) {
	 		adj[color[i - 1]][color[i]]++;
	 		adj[color[i]][color[i - 1]]++;
	 		ans++;
	 	}
	}

	cout << ans << endl;
	while(q--) {
		int a, b;
		cin >> a >> b;
		a = dsu.parent(a);
		b = dsu.parent(b);
		if(dsu.isConnected(a, b)) {
			cout << ans << endl;
			continue;
		}
		dsu.add(a, b);
		int var = dsu.parent(a);
		if(a != var) {
			swap(a, b);
		}
		// debug(adj[a], a);
		for(auto it : adj[a]) {
			if(dsu.isConnected(b, it.ff)) {
				ans -= adj[a][it.ff];
				adj[a].erase(it.ff);
			}
		}
		// ans -= adj[a][b];
		// adj[a].erase(b);
		for(auto it : adj[b]) {
			if(!dsu.isConnected(it.ff, a) and !dsu.isConnected(it.ff, b)) adj[a][dsu.parent(it.ff)] += it.ss;
		}
		for(auto it : adj[a]) {
			if(dsu.isConnected(it.ff, a)) adj[a].erase(it.ff);
		}

		// debug(adj[var], adj[b], var, b);
		adj[b].clear();
		// debug("l", adj[a], a);
		// for(auto it : adj[var]) {
		// 	if(it == b) {
		// 		ans--;
		// 	}
		// }

		cout << ans << endl;
	}
}


#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <iomanip>
#include <functional>
#include <cassert>
#include <bitset>
using namespace std;
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define FIO ios_base::sync_with_stdio(false);cin.tie(NULL)

//DEBUG

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef SHELTAURO
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

//CONSTANTS
const int N = 2e5 + 10;
const int mod = 1e9 + 7;

// do not start typing before getting the logic, please!!!
// stay calm

map <int, int> adj[N];

ll par[N + 1];
ll siz[N + 1];

void init(){
    for(ll i=0;i<N;i++){
        par[i] = i;
        siz[i] = 1;
    }
}

ll parent(ll n){
    while(n != par[n]){
        n = par[n];
        par[n] = par[par[n]];
    }
    return n;
}

void add(ll x, ll y){
    ll parx = parent(x);
    ll pary = parent(y);

    if(parx == pary) return;

    if(siz[parx] < siz[pary]){
        siz[pary] += siz[parx];
        par[parx] = pary;
    }
    else{
        siz[parx] += siz[pary];
        par[pary] = parx;
    }
}

bool isConnected(ll x,ll y){
    return (parent(x) == parent(y));
}

int main() {
	FIO;
	init();

	int n, q, ans = 0, a, b;
	cin >> n >> q;
	q--;
	vector <int> color(n);

	for(int i = 0; i < n; i++) {
	 	cin >> color[i];
	 	if(i > 0 and color[i] != color[i - 1]) {
	 		adj[color[i - 1]][color[i]]++;
	 		adj[color[i]][color[i - 1]]++;
	 		ans++;
	 	}
	}

	cout << ans << endl;
	while(q--) {

		cin >> a >> b;
		a = parent(a);
		b = parent(b);

		add(a, b);
		if(a != parent(a)) {
			swap(a, b);
		}
		// vector <int> res;
		// for(auto it : adj[a]) {
		// 	bool cond = parent(it.ff) == a;
		// 	if(cond) {
		// 		ans -= adj[a][it.ff];
		// 		res.pb(it.ff);
		// 	}
		// }
		// for(auto it : res) {
		// 	adj[a].erase(it);
		// }

		ans -= adj[a][b];
		adj[a].erase(b);
		// res.clear();

		for(auto it : adj[b]) {
			int val = parent(it.ff);
			bool cond = (val == a);
			if(!cond) {
				adj[a][val] += it.ss;
				adj[val][a] += it.ss;
				adj[val].erase(b);
			}
		}

		adj[b].clear();

		cout << ans << endl;
	}
}


#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <iomanip>
#include <functional>
#include <cassert>
#include <bitset>
using namespace std;
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define FIO ios_base::sync_with_stdio(false);cin.tie(NULL)

//DEBUG

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef SHELTAURO
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

//CONSTANTS
const int N = 2e5 + 2;
const int mod = 1e9 + 7;

// do not start typing before getting the logic, please!!!
// stay calm

unordered_map <int, int> adj[N];

int par[N + 1];
int siz[N + 1];

void init(){
    for(int i=0;i<N;i++){
        par[i] = i;
        siz[i] = 1;
    }
}

ll parent(ll n){
    while(n != par[n]){
        n = par[n];
        par[n] = par[par[n]];
    }
    return n;
}

void add(ll x, ll y){
    ll parx = parent(x);
    ll pary = parent(y);

    if(parx == pary) return;

    if(siz[parx] < siz[pary]){
        siz[pary] += siz[parx];
        par[parx] = pary;
    }
    else{
        siz[parx] += siz[pary];
        par[pary] = parx;
    }
}

bool isConnected(ll x,ll y){
    return (parent(x) == parent(y));
}

int main() {
	FIO;
	init();

	int n, q, ans = 0, a, b;
	cin >> n >> q;
	q--;
	vector <int> color(n);

	for(int i = 0; i < n; i++) {
	 	cin >> color[i];
	 	if(i > 0 and color[i] != color[i - 1]) {
	 		adj[color[i - 1]][color[i]]++;
	 		adj[color[i]][color[i - 1]]++;
	 		ans++;
	 	}
	}

	cout << ans << endl;
	while(q--) {

		cin >> a >> b;
		a = parent(a);
		b = parent(b);

		add(a, b);
		if(a != parent(a)) {
			swap(a, b);
		}

		ans -= adj[a][b];
		adj[a].erase(b);

		for(auto it : adj[b]) {
			int val = parent(it.ff);
			bool cond = (val == a);
			if(!cond) {
				adj[a][val] += it.ss;
				adj[val][a] += it.ss;
				adj[val].erase(b);
			}
		}

		adj[b].clear();

		cout << ans << endl;
	}
}


#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <iomanip>
#include <functional>
#include <cassert>
#include <bitset>
using namespace std;
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define FIO ios_base::sync_with_stdio(false);cin.tie(NULL)

//DEBUG

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef SHELTAURO
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

//CONSTANTS
const int N = 2e5 + 10;
const int mod = 1e9 + 7;

// do not start typing before getting the logic, please!!!
// stay calm

map <int, int> adj[N];

struct DSU{
    ll par[N + 1];
    ll size[N + 1];

    void init(){
        for(ll i=0;i<N;i++){
            par[i] = i;
            size[i] = 1;
        }
    }

    ll parent(ll n){
        while(n != par[n]){
            n = par[n];
            par[n] = par[par[n]];
        }
        return n;
    }

    void add(ll x, ll y){
        ll parx = parent(x);
        ll pary = parent(y);

        if(parx == pary) return;

        if(size[parx] < size[pary]){
            size[pary] += size[parx];
            par[parx] = pary;
        }
        else{
            size[parx] += size[pary];
            par[pary] = parx;
        }
    }

    bool isConnected(ll x,ll y){
        return (parent(x) == parent(y));
    }
}dsu;

int main() {
	FIO;
	dsu.init();

	int n, q, ans = 0;
	cin >> n >> q;
	q--;
	vector <int> color(n);

	for(int i = 0; i < n; i++) {
	 	cin >> color[i];
	 	if(i > 0 and color[i] != color[i - 1]) {
	 		adj[color[i - 1]][color[i]]++;
	 		adj[color[i]][color[i - 1]]++;
	 		ans++;
	 	}
	}

	cout << ans << endl;
	while(q--) {
		int a, b;
		cin >> a >> b;
		a = dsu.parent(a);
		b = dsu.parent(b);
		if(dsu.isConnected(a, b)) {
			cout << ans << endl;
			continue;
		}
		dsu.add(a, b);
		int var = dsu.parent(a);
		if(a != var) {
			swap(a, b);
		}
		// debug(adj[var], adj[b], var, b);
		ans -= adj[var][b];
		adj[var].erase(b);
		for(auto it : adj[b]) {
			if(!dsu.isConnected(it.ff, a)) adj[var][it.ff] += it.ss;
		}
		// debug(adj[var], adj[b], var, b);
		adj[b].clear();
		// for(auto it : adj[var]) {
		// 	if(it == b) {
		// 		ans--;
		// 	}
		// }

		cout << ans << endl;
	}
}


#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <iomanip>
#include <functional>
#include <cassert>
#include <bitset>
using namespace std;
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define FIO ios_base::sync_with_stdio(false);cin.tie(NULL)

//DEBUG

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef SHELTAURO
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

//CONSTANTS
const int N = 2e5 + 10;
const int mod = 1e9 + 7;

// do not start typing before getting the logic, please!!!
// stay calm

map <int, int> adj[N];

struct DSU{
    ll par[N + 1];
    ll size[N + 1];

    void init(){
        for(ll i=0;i<N;i++){
            par[i] = i;
            size[i] = 1;
        }
    }

    ll parent(ll n){
        while(n != par[n]){
            n = par[n];
            par[n] = par[par[n]];
        }
        return n;
    }

    void add(ll x, ll y){
        ll parx = parent(x);
        ll pary = parent(y);

        if(parx == pary) return;

        if(size[parx] < size[pary]){
            size[pary] += size[parx];
            par[parx] = pary;
        }
        else{
            size[parx] += size[pary];
            par[pary] = parx;
        }
    }

    bool isConnected(ll x,ll y){
        return (parent(x) == parent(y));
    }
}dsu;

int main() {
	FIO;
	dsu.init();

	int n, q, ans = 0;
	cin >> n >> q;
	q--;
	vector <int> color(n);

	for(int i = 0; i < n; i++) {
	 	cin >> color[i];
	 	if(i > 0 and color[i] != color[i - 1]) {
	 		adj[color[i - 1]][color[i]]++;
	 		adj[color[i]][color[i - 1]]++;
	 		ans++;
	 	}
	}

	cout << ans << endl;
	while(q--) {
		int a, b;
		cin >> a >> b;
		a = dsu.parent(a);
		b = dsu.parent(b);
		if(dsu.isConnected(a, b)) {
			cout << ans << endl;
			continue;
		}
		dsu.add(a, b);
		int var = dsu.parent(a);
		if(a != var) {
			swap(a, b);
		}
		// debug(adj[a], a);
		ans -= adj[a][b];
		adj[a].erase(b);
		for(auto it : adj[b]) {
			if(!dsu.isConnected(it.ff, a) and !dsu.isConnected(it.ff, b)) adj[a][dsu.parent(it.ff)] += it.ss;
		}
		for(auto it : adj[a]) {
			if(dsu.isConnected(it.ff, a)) adj[a].erase(it.ff);
		}

		// debug(adj[var], adj[b], var, b);
		adj[b].clear();
		// debug("l", adj[a], a);
		// for(auto it : adj[var]) {
		// 	if(it == b) {
		// 		ans--;
		// 	}
		// }

		cout << ans << endl;
	}
}


#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <iomanip>
#include <functional>
#include <cassert>
#include <bitset>
using namespace std;
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define FIO ios_base::sync_with_stdio(false);cin.tie(NULL)

//DEBUG

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef SHELTAURO
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

//CONSTANTS
const int N = 2e5 + 2;
const int mod = 1e9 + 7;

// do not start typing before getting the logic, please!!!
// stay calm

map <int, int> adj[N];

int par[N + 1];
int siz[N + 1];

void init(){
    for(int i=0;i<N;i++){
        par[i] = i;
        siz[i] = 1;
    }
}

ll parent(ll n){
    while(n != par[n]){
        n = par[n];
        par[n] = par[par[n]];
    }
    return n;
}

void add(ll x, ll y){
    ll parx = parent(x);
    ll pary = parent(y);

    if(parx == pary) return;

    if(siz[parx] < siz[pary]){
        siz[pary] += siz[parx];
        par[parx] = pary;
    }
    else{
        siz[parx] += siz[pary];
        par[pary] = parx;
    }
}

bool isConnected(ll x,ll y){
    return (parent(x) == parent(y));
}

int main() {
	FIO;
	init();

	int n, q, ans = 0, a, b;
	cin >> n >> q;
	q--;
	vector <int> color(n);

	for(int i = 0; i < n; i++) {
	 	cin >> color[i];
	 	if(i > 0 and color[i] != color[i - 1]) {
	 		adj[color[i - 1]][color[i]]++;
	 		adj[color[i]][color[i - 1]]++;
	 		ans++;
	 	}
	}

	cout << ans << endl;
	while(q--) {

		cin >> a >> b;
		a = parent(a);
		b = parent(b);

		add(a, b);
		if(a != parent(a)) {
			swap(a, b);
		}

		ans -= adj[a][b];
		adj[a].erase(b);

		for(auto it : adj[b]) {
			int val = parent(it.ff);
			bool cond = (val == a);
			if(!cond) {
				adj[a][val] += it.ss;
				adj[val][a] += it.ss;
				adj[val].erase(b);
			}
		}

		adj[b].clear();

		cout << ans << endl;
	}
}


#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <iomanip>
#include <functional>
#include <cassert>
#include <bitset>
using namespace std;
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define FIO ios_base::sync_with_stdio(false);cin.tie(NULL)

//DEBUG

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef SHELTAURO
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

//CONSTANTS
const int N = 2e5 + 10;
const int mod = 1e9 + 7;

// do not start typing before getting the logic, please!!!
// stay calm

map <int, int> adj[N];

struct DSU{
    ll par[N + 1];
    ll size[N + 1];

    void init(){
        for(ll i=0;i<N;i++){
            par[i] = i;
            size[i] = 1;
        }
    }

    ll parent(ll n){
        while(n != par[n]){
            n = par[n];
            par[n] = par[par[n]];
        }
        return n;
    }

    void add(ll x, ll y){
        ll parx = parent(x);
        ll pary = parent(y);

        if(parx == pary) return;

        if(size[parx] < size[pary]){
            size[pary] += size[parx];
            par[parx] = pary;
        }
        else{
            size[parx] += size[pary];
            par[pary] = parx;
        }
    }

    bool isConnected(ll x,ll y){
        return (parent(x) == parent(y));
    }
}dsu;

int main() {
	FIO;
	dsu.init();

	int n, q, ans = 0;
	cin >> n >> q;
	q--;
	vector <int> color(n);

	for(int i = 0; i < n; i++) {
	 	cin >> color[i];
	 	if(i > 0 and color[i] != color[i - 1]) {
	 		adj[color[i - 1]][color[i]]++;
	 		adj[color[i]][color[i - 1]]++;
	 		ans++;
	 	}
	}

	cout << ans << endl;
	while(q--) {
		int a, b;
		cin >> a >> b;
		a = dsu.parent(a);
		b = dsu.parent(b);
		if(dsu.isConnected(a, b)) {
			cout << ans << endl;
			continue;
		}
		dsu.add(a, b);
		int var = dsu.parent(a);
		if(a != var) {
			swap(a, b);
		}
		// debug(adj[var], adj[b], var, b);
		ans -= adj[var][b];
		adj[var].erase(b);
		for(auto it : adj[b]) {
			if(!dsu.isConnected(it.ff, a) and !dsu.isConnected(it.ff, b)) adj[var][it.ff] += it.ss;
		}
		for(auto it : adj[var]) {
			if(dsu.isConnected(it.ff, a)) adj[var].erase(it.ff);
		}

		// debug(adj[var], adj[b], var, b);
		adj[b].clear();
		// for(auto it : adj[var]) {
		// 	if(it == b) {
		// 		ans--;
		// 	}
		// }

		cout << ans << endl;
	}
}


#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <iomanip>
#include <functional>
#include <cassert>
#include <bitset>
using namespace std;
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define FIO ios_base::sync_with_stdio(false);cin.tie(NULL)

//DEBUG

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef SHELTAURO
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

//CONSTANTS
const int N = 2e5 + 10;
const int mod = 1e9 + 7;

// do not start typing before getting the logic, please!!!
// stay calm

map <int, int> adj[N];

struct DSU{
    ll par[N + 1];
    ll size[N + 1];

    void init(){
        for(ll i=0;i<N;i++){
            par[i] = i;
            size[i] = 1;
        }
    }

    ll parent(ll n){
        while(n != par[n]){
            n = par[n];
            par[n] = par[par[n]];
        }
        return n;
    }

    void add(ll x, ll y){
        ll parx = parent(x);
        ll pary = parent(y);

        if(parx == pary) return;

        if(size[parx] < size[pary]){
            size[pary] += size[parx];
            par[parx] = pary;
        }
        else{
            size[parx] += size[pary];
            par[pary] = parx;
        }
    }

    bool isConnected(ll x,ll y){
        return (parent(x) == parent(y));
    }
}dsu;

int main() {
	FIO;
	dsu.init();

	int n, q, ans = 0;
	cin >> n >> q;
	q--;
	vector <int> color(n);

	for(int i = 0; i < n; i++) {
	 	cin >> color[i];
	 	if(i > 0 and color[i] != color[i - 1]) {
	 		adj[color[i - 1]][color[i]]++;
	 		adj[color[i]][color[i - 1]]++;
	 		ans++;
	 	}
	}

	cout << ans << endl;
	while(q--) {
		int a, b;
		cin >> a >> b;
		a = dsu.parent(a);
		b = dsu.parent(b);
		if(dsu.isConnected(a, b)) {
			cout << ans << endl;
			continue;
		}
		dsu.add(a, b);
		if(a != dsu.parent(a)) {
			swap(a, b);
		}
		vector <int> res;
		for(auto it : adj[a]) {
			if(dsu.isConnected(b, it.ff)) {
				ans -= adj[a][it.ff];
				res.pb(it.ff);
			}
		}
		for(auto it : res) {
			adj[a].erase(it);
		}
		res.clear();

		for(auto it : adj[b]) {
			if(!dsu.isConnected(it.ff, a)) adj[a][dsu.parent(it.ff)] += it.ss;
		}

		// debug(adj[var], adj[b], var, b);
		adj[b].clear();
		// debug("l", adj[a], a);
		// for(auto it : adj[var]) {
		// 	if(it == b) {
		// 		ans--;
		// 	}
		// }

		cout << ans << endl;
	}
}


#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <iomanip>
#include <functional>
#include <cassert>
#include <bitset>
using namespace std;
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define FIO ios_base::sync_with_stdio(false);cin.tie(NULL)

//DEBUG

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef SHELTAURO
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

//CONSTANTS
const int N = 2e5 + 10;
const int mod = 1e9 + 7;

// do not start typing before getting the logic, please!!!
// stay calm

map <int, int> adj[N];

struct DSU{
    ll par[N + 1];
    ll size[N + 1];

    void init(){
        for(ll i=0;i<N;i++){
            par[i] = i;
            size[i] = 1;
        }
    }

    ll parent(ll n){
        while(n != par[n]){
            n = par[n];
            par[n] = par[par[n]];
        }
        return n;
    }

    void add(ll x, ll y){
        ll parx = parent(x);
        ll pary = parent(y);

        if(parx == pary) return;

        if(size[parx] < size[pary]){
            size[pary] += size[parx];
            par[parx] = pary;
        }
        else{
            size[parx] += size[pary];
            par[pary] = parx;
        }
    }

    bool isConnected(ll x,ll y){
        return (parent(x) == parent(y));
    }
}dsu;

int main() {
	FIO;
	dsu.init();

	int n, q, ans = 0;
	cin >> n >> q;
	q--;
	vector <int> color(n);

	for(int i = 0; i < n; i++) {
	 	cin >> color[i];
	 	if(i > 0 and color[i] != color[i - 1]) {
	 		adj[color[i - 1]][color[i]]++;
	 		adj[color[i]][color[i - 1]]++;
	 		ans++;
	 	}
	}

	cout << ans << endl;
	while(q--) {
		int a, b;
		cin >> a >> b;
		a = dsu.parent(a);
		b = dsu.parent(b);
		if(dsu.isConnected(a, b)) {
			cout << ans << endl;
			continue;
		}
		dsu.add(a, b);
		int var = dsu.parent(a);
		if(a != var) {
			swap(a, b);
		}
		// debug(adj[a], a);
		vector <int> res;
		for(auto it : adj[a]) {
			if(dsu.isConnected(b, it.ff)) {
				ans -= adj[a][it.ff];
				res.pb(it.ff);
				// adj[a].erase(it.ff);
			}
		}
		for(auto it : res) {
			adj[a].erase(it);
		}
		res.clear();
		// ans -= adj[a][b];
		// adj[a].erase(b);
		for(auto it : adj[b]) {
			if(!dsu.isConnected(it.ff, a) and !dsu.isConnected(it.ff, b)) adj[a][dsu.parent(it.ff)] += it.ss;
		}
		for(auto it : adj[a]) {
			if(dsu.isConnected(it.ff, a)) adj[a].erase(it.ff);
		}

		// debug(adj[var], adj[b], var, b);
		adj[b].clear();
		// debug("l", adj[a], a);
		// for(auto it : adj[var]) {
		// 	if(it == b) {
		// 		ans--;
		// 	}
		// }

		cout << ans << endl;
	}
}


#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define prl(x) cout << x << endl
#define prld(x, y) cout << fixed << setprecision(y) << x << endl
#define pr(x) cout << x
#define rep(i,a,n) for (int i=a;i<n;i++)
#define gcd __gcd
#define ggg(x1, x2) gcd(abs(x1), abs(x2))
#define numOfBits(x) __builtin_popcount(x)
#define msb(x) 63-__builtin_clzll(x)
#define speed ios::sync_with_stdio(0)
#define epsenom 0.0000001
#define mod 1000000007
#define inf 1000000000000000000ll
using namespace std;
int main()
{
    speed;
    ll n, m;
    cin >> n >> m;
    vector<ll> v[m+10];
    map<ll, ll> con;
    rep(i, 1, n+1)
    {
    	ll in;
    	cin >> in;
    	v[in].pb(i);
    	con[i] = i;
	}
	set<pair<ll, bool> > s[m+10];
	ll ans = 0;
	rep(i, 1, m+1)
	{
		ll st = v[i][0];
		rep(j, 1, v[i].size())
		{
			if(v[i][j]-1!=v[i][j-1])
			{
				s[i].insert(mp(st, 0));
				s[i].insert(mp(v[i][j-1], 1));
				st = v[i][j];
			}
		}
		s[i].insert(mp(st, 0));
		s[i].insert(mp(v[i][v[i].size()-1], 1));
		ans += (s[i].size()/2) - 1;
	}
	ans += m-1;
	prl(ans);
	rep(i, 0, m-1)
	{
		ll a, b;
		cin >> a >> b;
		ll ra = a;
		ll rb = b;
		a = con[a];
		b = con[b];
		if(s[a].size()<s[b].size())
			swap(a, b);
		ans -= (s[a].size()/2)-1;
		ans -= (s[b].size()/2);
		set<pair<ll, ll> > r;
		while(s[b].size())
		{
			pair<ll, bool> cur1 = *s[b].begin();
			s[a].insert(cur1);
			s[b].erase(s[b].begin());
			pair<ll, bool> cur2 = *s[b].begin();
			s[a].insert(cur2);
			s[b].erase(s[b].begin());
			if(s[a].find(mp(cur1.first-1, 1))!=s[a].end())
			{
				s[a].erase(mp(cur1.first-1, 1));
				s[a].erase(cur1);
			}
			if(s[a].find(mp(cur2.first+1, 0))!=s[a].end())
			{
				s[a].erase(mp(cur2.first+1, 0));
				s[a].erase(cur2);
			}
		}
		ans += (s[a].size()/2)-1;
		if(s[con[ra]].size()<s[con[rb]].size())
		{
			con[ra] = con[rb];
		}
		prl(ans);
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define prl(x) cout << x << endl
#define prld(x, y) cout << fixed << setprecision(y) << x << endl
#define pr(x) cout << x
#define rep(i,a,n) for (int i=a;i<n;i++)
#define gcd __gcd
#define ggg(x1, x2) gcd(abs(x1), abs(x2))
#define numOfBits(x) __builtin_popcount(x)
#define msb(x) 63-__builtin_clzll(x)
#define speed ios::sync_with_stdio(0)
#define epsenom 0.0000001
#define mod 1000000007
#define inf 1000000000000000000ll
using namespace std;
int main()
{
    speed;
    ll n, m;
    cin >> n >> m;
    vector<ll> v[m+10];
    rep(i, 1, n+1)
    {
    	ll in;
    	cin >> in;
    	v[in].pb(i);
	}
	set<pair<ll, ll> > s[m+10];
	ll ans = 0;
	rep(i, 1, m+1)
	{
		ll st = v[i][0];
		rep(j, 1, v[i].size())
		{
			if(v[i][j]-1!=v[i][j-1])
			{
				s[i].insert(mp(st, v[i][j-1]));
				st = v[i][j];
			}
		}
		s[i].insert(mp(st, v[i][v[i].size()-1]));
		ans += s[i].size() - 1;
	}
	ans += m-1;
	prl(ans);

	/*
	rep(i, 1, m+1)
	{
		prl(i << ": ");
		for(auto ss : s[i])
		{
			prl(ss.first << " " << ss.second);
		}
	}
	*/
	rep(i, 0, m-1)
	{
		ll a, b;
		cin >> a >> b;
		ans -= s[a].size()-1;
		ans -= s[b].size();
		set<pair<ll, ll> > r;
		for(auto ss : s[b])
			r.insert(ss);
		for(auto ss : s[a])
			r.insert(ss);
		s[b].clear();
		s[a].clear();
		pair<ll, ll> is = *r.begin();
		r.erase(r.begin());
		while(r.size())
		{
			if((*r.begin()).first-1==is.second)
			{
				is.second = (*r.begin()).second;
				r.erase(r.begin());
			}
			else
			{
				s[a].insert(is);
				is = *r.begin();
				r.erase(r.begin());
			}
		}
		s[a].insert(is);
		ans += s[a].size()-1;
		prl(ans);
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define prl(x) cout << x << endl
#define prld(x, y) cout << fixed << setprecision(y) << x << endl
#define pr(x) cout << x
#define rep(i,a,n) for (int i=a;i<n;i++)
#define gcd __gcd
#define ggg(x1, x2) gcd(abs(x1), abs(x2))
#define numOfBits(x) __builtin_popcount(x)
#define msb(x) 63-__builtin_clzll(x)
#define speed ios::sync_with_stdio(0)
#define epsenom 0.0000001
#define mod 1000000007
#define inf 1000000000000000000ll
using namespace std;
int main()
{
    speed;
    ll n, m;
    cin >> n >> m;
    vector<ll> v[m+10];
    rep(i, 1, n+1)
    {
    	ll in;
    	cin >> in;
    	v[in].pb(i);
	}
	set<pair<ll, bool> > s[m+10];
	ll ans = 0;
	rep(i, 1, m+1)
	{
		ll st = v[i][0];
		rep(j, 1, v[i].size())
		{
			if(v[i][j]-1!=v[i][j-1])
			{
				s[i].insert(mp(st, 0));
				s[i].insert(mp(v[i][j-1], 1));
				st = v[i][j];
			}
		}
		s[i].insert(mp(st, 0));
		s[i].insert(mp(v[i][v[i].size()-1], 1));
		ans += (s[i].size()/2) - 1;
	}
	ans += m-1;
	prl(ans);

	/*
	rep(i, 1, m+1)
	{
		prl(i << ": ");
		for(auto ss : s[i])
		{
			prl(ss.first << " " << ss.second);
		}
	}
	*/
	rep(i, 0, m-1)
	{
		ll a, b;
		cin >> a >> b;
		ans -= (s[a].size()/2)-1;
		ans -= (s[b].size()/2);
		set<pair<ll, ll> > r;
		while(s[b].size())
		{
			pair<ll, bool> cur1 = *s[b].begin();
			s[a].insert(cur1);
			s[b].erase(s[b].begin());
			pair<ll, bool> cur2 = *s[b].begin();
			s[a].insert(cur2);
			s[b].erase(s[b].begin());
			if(s[a].find(mp(cur1.first-1, 1))!=s[a].end())
			{
				s[a].erase(mp(cur1.first-1, 1));
				s[a].erase(cur1);
			}
			if(s[a].find(mp(cur2.first+1, 0))!=s[a].end())
			{
				s[a].erase(mp(cur2.first+1, 0));
				s[a].erase(cur2);
			}
		}
		ans += (s[a].size()/2)-1;
		prl(ans);
	}
	return 0;
}