import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons, std.random;

void main() {
    const tmp = readln.split.to!(long[]);
    auto N = tmp[0], M = tmp[1];
    auto ps = readln.split.to!(long[]);
    auto pairs = M.iota.map!(_ => readln.split.to!(long[])).array;

    auto nodeFromIndex = new Node[N];
    auto nodeFromValue = new Node[N];
    foreach (i; 0..N) {
        auto n = new Node(i);
        nodeFromIndex[i] = n;
        nodeFromValue[ps[i]-1] = n;
    }

    foreach (i; 0..M) {
        nodeFromIndex[pairs[i][0]-1].unite(nodeFromIndex[pairs[i][1]-1]);
    }

    long cnt;
    foreach (i; 0..N) {
        if (nodeFromIndex[i].find(nodeFromValue[i]))
            cnt++;
    }
    writeln(cnt);
}

class Node {
    long value;
    Node parent;

    this(long value) {
        this.value = value;
    }

    void unite(Node n) {
        if (this.find(n)) return;
        n.root.parent = this.root;
    }

    bool find(Node n) {
        return this.root.value == n.root.value;
    }

    Node root() {
        if (parent is null) return this;
        auto res = parent.root;
        this.parent = res;
        return res;
    }
}
