import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.NoSuchElementException;
import java.util.TreeSet;
import java.util.ArrayList;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		Scanner in = new Scanner(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskD solver = new TaskD();
		solver.solve(1, in, out);
		out.close();
	}

	static class TaskD {
		public void solve(int testNumber, Scanner sc, PrintWriter out) {

			int n = sc.nextInt();
			int m = sc.nextInt();
			int[] p = new int[n];
			int[] pos = new int[n];
			for (int i = 0; i < n; i++) {
				p[i] = sc.nextInt() - 1;
				pos[p[i]] = i;
			}

			ArrayList<TreeSet<Integer>> al = new ArrayList<>();
			for (int i = 0; i < n; i++) {
				TreeSet<Integer> ts = new TreeSet<>();
				ts.add(i);
				al.add(ts);
			}
			for (int i = 0; i < m; i++) {
				int x = sc.nextInt() - 1;
				int y = sc.nextInt() - 1;
				al.get(x).add(y);
				al.get(y).add(x);
			}

			boolean[] check = new boolean[n];
			for (int i = 0; i < n; i++) {
				if (check[i]) continue;
				TreeSet<Integer> ts = new TreeSet<>();
				ArrayDeque<Integer> q = new ArrayDeque<>();
				q.offer(i);
				while (!q.isEmpty()) {
					int j = q.poll();
					ts.add(j);
					check[j] = true;
					for (Integer k : al.get(j)) {
						if (!check[k]) q.offer(k);
					}
				}
				for (Integer j : ts) {
					al.set(j, ts);
				}
			}

			int ans = 0;
			for (int i = 0; i < n; i++) {
				if (al.get(i).contains(pos[i])) {
					ans++;
				}
			}

			out.println(ans);

		}

	}

	static class Scanner {
		private final InputStream in;
		private final byte[] buffer;
		private int ptr;
		private int buflen;

		public Scanner(InputStream in) {
			this.in = in;
			this.buffer = new byte[1024];
			this.ptr = 0;
			this.buflen = 0;
		}

		private boolean hasNextByte() {
			if (ptr < buflen) return true;
			else {
				ptr = 0;
				try {
					buflen = in.read(buffer);
				} catch (IOException e) {
					e.printStackTrace();
				}
				if (buflen <= 0) return false;
			}
			return true;
		}

		private byte readByte() {
			if (hasNextByte()) return buffer[ptr++];
			return -1;
		}

		private boolean isPrintableChar(byte c) {
			return '!' <= c && c <= '~';
		}

		private void skipUnprintable() {
			while (hasNextByte() && !isPrintableChar(buffer[ptr])) {
				ptr++;
			}
		}

		public boolean hasNext() {
			skipUnprintable();
			return hasNextByte();
		}

		public int nextInt() {
			if (!hasNext()) throw new NoSuchElementException();
			int n = 0;
			boolean minus = false;
			byte b = readByte();
			if (b == '-') {
				minus = true;
				b = readByte();
			}
			if (b < '0' || '9' < b) throw new NumberFormatException();
			while (true) {
				if ('0' <= b && b <= '9') {
					n *= 10;
					n += b - '0';
				} else if (b == -1 || !isPrintableChar(b)) {
					return minus ? -n : n;
				} else {
					throw new NumberFormatException();
				}
				b = readByte();
			}
		}

	}
}



Measured memory_bytes: 104568