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

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

    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) {
        tmp = readln.split.to!(long[]);
        const x = tmp[0], y = tmp[1];
        nodeFromIndex[x-1].unite(nodeFromIndex[y-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) {
        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 result = parent.root;
        this.parent = result;
        return result;
    }
}
