using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace AtCorder
{
	public class Program
	{
		static void Main(string[] args)
		{
			var cin = new Scanner2();
			int n = cin.Int();
			int m = cin.Int();
			var p = cin.ArrayInt(n, - 1);

			var uf = new UnionFindTree(n);
			for (int i = 0; i < m; i++) {
				int x = cin.Int() - 1;
				int y = cin.Int() - 1;
				uf.TryUnite(x, y);
			}

			int ans = 0;
			for (int i = 0; i < n; i++) {
				if (uf.IsUnited(i, p[i])) {
					++ans;
				}
			}

			Console.WriteLine(ans);
		}
	}

	public class UnionFindTree
	{
		private readonly List<int> sizes_;
		private readonly List<int> parents_;

		public int Count { get; private set; }
		public int GroupCount { get; private set; }

		public UnionFindTree(int count)
		{
			parents_ = new List<int>(count);
			sizes_ = new List<int>(count);

			Count = 0;
			GroupCount = 0;

			ExtendSize(count);
		}

		public bool IsUnited(int x, int y)
		{
			int xRoot = Find(x);
			int yRoot = Find(y);

			return xRoot == yRoot;
		}

		public bool TryUnite(int x, int y)
		{
			int xRoot = Find(x);
			int yRoot = Find(y);
			if (yRoot == xRoot) {
				return false;
			}

			int parent = sizes_[xRoot] < sizes_[yRoot] ? yRoot : xRoot;
			int child = sizes_[xRoot] < sizes_[yRoot] ? xRoot : yRoot;

			GroupCount--;
			parents_[child] = parent;
			sizes_[parent] += sizes_[child];

			return true;
		}
		public IEnumerable<int> AllRepresents => parents_.Where(x => x == parents_[x]);
		public int GetSize(int x) => sizes_[Find(x)];

		public int Find(int x)
		{
			while (x != parents_[x]) {
				parents_[x] = parents_[parents_[x]];
				x = parents_[x];
			}

			return x;
		}

		public void ExtendSize(int treeSize)
		{
			if (treeSize <= Count) {
				return;
			}

			parents_.Capacity = treeSize;
			sizes_.Capacity = treeSize;
			while (Count < treeSize) {
				parents_.Add(Count);
				sizes_.Add(1);
				Count++;
				GroupCount++;
			}
		}
	}

	public static class Helper
	{
		public static void UpdateMin32<T>(T array, int index, int value) where T : IList<int>
			=> array[index] = Math.Min(array[index], value);
		public static void UpdateMax32<T>(T array, int index, int value) where T : IList<int>
			=> array[index] = Math.Max(array[index], value);
		public static void UpdateMin64<T>(T array, int index, long value) where T : IList<long>
			=> array[index] = Math.Min(array[index], value);
		public static void UpdateMax64<T>(T array, int index, long value) where T : IList<long>
			=> array[index] = Math.Max(array[index], value);

		public static T[] Array<T>(int n, Func<int, T> init)
			=> Enumerable.Range(0, n).Select(x => init(x)).ToArray();
		public static List<T> List<T>(int n, Func<int, T> init)
			=> Enumerable.Range(0, n).Select(x => init(x)).ToList();
	}

	public class Scanner2
	{
		private readonly char[] delimiter_ = new char[] { ' ' };
		private readonly string filePath_;
		private readonly Func<string> reader_;
		private string[] buf_;
		private int index_;

		public Scanner2(string file = "")
		{
			if (string.IsNullOrWhiteSpace(file)) {
				reader_ = Console.ReadLine;
			} else {
				filePath_ = file;
				var fs = new StreamReader(file);
				reader_ = fs.ReadLine;
			}
			buf_ = new string[0];
			index_ = 0;
		}

		public string Next()
		{
			if (index_ < buf_.Length) {
				return buf_[index_++];
			}

			string st = reader_();
			while (st == "") {
				st = reader_();
			}

			buf_ = st.Split(delimiter_, StringSplitOptions.RemoveEmptyEntries);
			if (buf_.Length == 0) {
				return Next();
			}

			index_ = 0;
			return buf_[index_++];
		}

		public int Int() => int.Parse(Next());
		public long Long() => long.Parse(Next());
		public double Double() => double.Parse(Next());

		public int[] ArrayInt(int N, int add = 0)
		{
			int[] Array = new int[N];
			for (int i = 0; i < N; i++) {
				Array[i] = Int() + add;
			}
			return Array;
		}

		public long[] ArrayLong(int N, long add = 0)
		{
			long[] Array = new long[N];
			for (int i = 0; i < N; i++) {
				Array[i] = Long() + add;
			}
			return Array;
		}

		public double[] ArrayDouble(int N, double add = 0)
		{
			double[] Array = new double[N];
			for (int i = 0; i < N; i++) {
				Array[i] = Double() + add;
			}
			return Array;
		}

		public void Save(string text)
		{
			if (string.IsNullOrWhiteSpace(filePath_)) {
				return;
			}

			File.WriteAllText(filePath_ + "_output.txt", text);
		}
	}
}

Measured memory_bytes: 25032