void main()
{
    long[] tmp = readln.split.to!(long[]);
    long n = tmp[0], m = tmp[1];
    long[] p = readln.split.to!(long[]);
    p[] -= 1;

    auto tree = UnionFind(n);
    foreach (i; 0 .. m)
    {
        tmp = readln.split.to!(long[]);
        long x = tmp[0] - 1, y = tmp[1] - 1;
        tree.unite(p[x], p[y]);
    }

    long cnt;
    foreach (i; 0 .. n)
    {
        if (tree.same(i, p[i])) ++cnt;
    }

    cnt.writeln;
}

struct UnionFind
{
    long[] par;

    this(long n)
    {
        par = new long[n];
        foreach (i; 0 .. n)
        {
            par[i] = i;
        }
    }

    long root(long x)
    {
        if (par[x] == x) return x;
        else return par[x] = root(par[x]);
    }

    bool same(long x, long y)
    {
        return root(x) == root(y);
    }

    void unite(long x, long y)
    {
        x = root(x), y = root(y);
        if (x == y) return;
        par[x] = y;
    }
}

import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;