/*
    In the name of
        "ALLAH"
    And say "AL-HAMD-U-LILLAH"
*/

#include<bits/stdc++.h>
#define   ll   long long
#define   llu  unsigned long long
#define   _    printf("\n")
int      take() {
    int n;
    scanf("%d",&n);
    return n;

}
double  ttake() {
    double n;
    scanf("%lf",&n);
    return n;
}
ll     takes() {
    ll n;
    scanf("%lld",&n);
    return n;

}

#define  Int            take()
#define  Dnt            ttake()
#define  LL             takes()
#define  done           {return 0;}
#define  fr(i,x,n,a)    for(int i=x;i<=n;i+=a)
#define  FR(a,b,x)      for(int i=(a);i<=(b);i+=(x))
#define  rfr(i,x,n,a)   for(int i=x;i>=n;i--)
int      cas;
#define  print          printf("Case %d:",++cas)
#define  vi             vector<int>
#define  vl              vector<ll>
#define  pb              push_back
#define  pii             pair<int,int>
#define  vii             vector<pii>
#define  pll             pair<ll,ll>
#define  pil             pair<int,ll>
#define  pli             pair<ll,int>
#define  mk              make_pair
#define  fill(x,v)       memset(x,v,sizeof x)

using    namespace      std;

bool     approximatelyEqual(float a, float b, float epsilon) {
    return fabs(a - b) <= ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
}

bool     essentiallyEqual(float a, float b, float epsilon) {
    return fabs(a - b) <= ( (fabs(a) > fabs(b) ? fabs(b) : fabs(a)) * epsilon);
}

bool     definitelyGreaterThan(float a, float b, float epsilon) {
    return (a - b) > ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
}

bool     definitelyLessThan(float a, float b, float epsilon) {
    return (b - a) > ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
}
#define  STR            {struct info}
#define  dbg(x)         {cout<<"ekhane"<<x<<endl;}
#define  SS             second
#define  FF             first
#define  PQ             priority_queue
#define  great          greatest
#define  less           lesser
#define  mii            map<int,int>
#define  mil            map<int,ll>
#define  mll            map<ll,ll>
#define  msb            map<string,bool>
#define  V              vector
#define  M              100005
#define  pf             printf
//////////////////////////////////////////
int arr[ M ] , par[ M ] ;

void init( ){
    fr( i , 1 , M - 1 , 1 ) par[ i ] = i ;
}

int root( int x ){
    if( par[ x ] != x ) par[ x ] = root( par[ par[ x ] ] ) ;
    return par[ x ] ;
}
void Union( int x , int y ){
    int rx = root( x ) , ry = root( y ) ;
    if( rx != ry ){
        par[ rx ] = ry ;
    }
}
int main(){
    init( ) ;
   int n = Int , m = Int , u , v ;

   fr( i , 1 , n , 1 ) arr[ i ] = Int ;
   fr( i , 1 , m , 1 ){
        u = Int , v = Int ;
        Union( u , v ) ;
   }
   vi v1[ n + 1 ] , val[ n + 1 ];

   fr( i , 1 , n , 1 ){
       u = root( i ) ;
      v1[ u ].pb( i ) ;
      val[ u ].pb( arr[ i ] ) ;
   }
   int ans = 0 ;
    fr( i , 1 , n , 1 ){
        if( root( i ) == i ){

            sort( v1[ i ].begin( ) , v1[ i ].end( ) );
            sort( val[ i ].begin( ) , val[ i ].end( ) );
            val[ i ].pb( INT_MAX ) ;
            int ind = 0 , k = 0 , len = v1[ i ].size( ) - 1;
            while( k <= len ){
                u = v1[ i ][ k ] ;

                if( u == val[ i][ind] ) {
                        ans++ ;
                    ind++ ;k++ ;
                }else if( u > val[ i ][ ind ] ){
                    ind++ ;
                }else{
                    k++ ;
                }
            }
        }
   }
   pf("%d\n",ans) ;
   done;
}





Measured memory_bytes: 11904