unittest
{
	assert( [ "5 2", "5 3 1 4 2", "1 3", "5 4" ].parse.expand.solve == 2 );
	assert( [ "3 2", "3 2 1", "1 2", "2 3" ].parse.expand.solve == 3 );
	assert( [ "10 8", "5 3 6 8 7 10 9 1 2 4", "3 1", "4 1", "5 9", "2 5", "6 5", "3 5", "8 9", "7 9" ].parse.expand.solve == 8 );
	assert( [ "5 1", "1 2 3 4 5", "1 5" ].parse.expand.solve == 5 );
}



import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.typecons;

void main()
{
	stdin.byLineCopy.parse.expand.solve.writeln;
}

auto parse( Range )( Range input )
if( isInputRange!Range && is( ElementType!Range == string ) )
{
	auto nm = input.front.split.to!( long[] );
	input.popFront;
	auto ps = input.front.split.to!( long[] );
	ps[] -= 1;
	auto xys = new long[ 2 ][ nm[ 1 ] ];
	foreach( ref xy; xys )
	{
		input.popFront;
		xy = input.front.split.to!( long[] );
		xy[] -= 1;
	}
	return tuple( ps, xys );
}

auto solve( long[] ps, long[ 2 ][] xys )
{
	// 入れ替えできるグループを判定
	auto uft = UnionFindTree( ps.length );
	foreach( xy; xys )
	{
		uft.unite( xy[ 0 ], xy[ 1 ] );
	}
	
	// グループごとにpiを収集
	auto bss = new long[][ ps.length ];
	foreach( i; 0L .. ps.length.signed )
	{
		bss[ uft.root( i ) ] ~= ps[ i ];
	}
	foreach( ref bs; bss )
	{
		bs.sort();
	}
	
	// i == pi にできる数を集計
	auto ans = 0L;
	foreach( i; 0L .. ps.length.signed )
	{
		auto r = uft.root( i );
		while( !bss[ r ].empty && bss[ r ].front < i )
		{
			bss[ r ].popFront;
		}
		ans += ( !bss[ r ].empty && bss[ r ].front == i );
	}
	return ans;
}

// UnionFind木
struct UnionFindTree
{
	private long[] ps;
	private long[] rs;
	
	// 重み無し初期化
	public this( long n )
	{
		ps.length = n;
		ps[] = -1;
		rs.length = n;
	}
	
	public long root( long i )
	{
		if( ps[ i ] < 0 ) return i;
		else              return ps[ i ] = root( ps[ i ] );
	}
	
	public bool same( long i, long j )
	{
		return root( i ) == root( j );
	}
	
	public void unite( long i, long j )
	{
		i = root( i );
		j = root( j );
		if( i == j ) return;
		if( rs[ i ] < rs[ j ] )
		{
			ps[ j ] += ps[ i ];
			ps[ i ] = j;
		}
		else
		{
			ps[ i ] += ps[ j ];
			ps[ j ] = i;
			if( rs[ i ] == rs[ j ] ) rs[ i ]++;
		}
	}
}
