using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Text;

public class Program
{

    public void Proc()
    {

		int[] inpt = Reader.ReadLine().Split(' ').Select(a => int.Parse(a)).ToArray();
		this.Num = inpt[0];
		int roadNum = inpt[1];
		this.NumList = Reader.ReadLine().Split(' ').Select(a => int.Parse(a) - 1).ToArray();
		this.NodeList = new TreeNode[this.Num];
		List<int[]> road = new List<int[]>();
		for (int i = 0; i < roadNum; i++) {
			inpt = Reader.ReadLine().Split(' ').Select(a => int.Parse(a) - 1).OrderBy(a=>a).ToArray();
			road.Add(inpt);
		}
		road.Sort((a, b) =>
		{
			if(a[0]==b[0]) {
				return a[1].CompareTo(b[1]);
			} else {
				return a[0].CompareTo(b[0]);
			}
		});

		for (int i = 0; i < road.Count; i++) {
			int a = road[i][0];
			int b = road[i][1];
			if(NodeList[a] == null) {
				NodeList[a] = new TreeNode(a);
				if(NodeList[b] == null) {
					NodeList[b] = new TreeNode(b);
					NodeList[a].Add(NodeList[b]);
				} else {
					NodeList[b].Parent.Add(NodeList[a]);
				}

			} else {
				if (NodeList[b] == null)
                {
                    this.NodeList[b] = new TreeNode(b);
					NodeList[a].Parent.Add(NodeList[b]);
				} else if(NodeList[a].GetParent().Idx != NodeList[b].GetParent().Idx) {
					NodeList[a].Parent.Add(NodeList[b].Parent);
				}
			}
		}
		int[] pos = new int[this.Num];
		for (int i = 0; i < this.Num; i++) {
			pos[NumList[i]] = i;
		}
		int ans = 0;
		for (int i = 0; i < this.Num; i++) {
			if(pos[i] == i) {
				ans++;
				continue;
			}
			int idx1 = pos[i];
			int idx2 = i;

			int num1 = i;
			int num2 = NumList[idx2];
		
			if(NodeList[idx1] == null || NodeList[idx2] == null) {
				continue;
			}
			if(NodeList[idx1].GetParent().Idx != NodeList[idx2].GetParent().Idx) {
				continue;
			}
			NumList[idx2] = i;
			NumList[idx1] = num2;
			pos[i] = i;
			pos[num2] = idx1;
			ans++;
		}
		Console.WriteLine(ans);
    }

	private int[] NumList;

	private int Num;

	private TreeNode[] NodeList;
	private class TreeNode {
		public int Idx;
		public TreeNode Parent;
		public List<TreeNode> Childs = new List<TreeNode>();
		public TreeNode(int id) {
			this.Parent = this;
			this.Idx = id;
		}
		public void Add(TreeNode child) {
			this.Childs.Add(child);
			child.Parent = this.Parent;
		}
		public TreeNode GetParent() {
			if(this.Parent == this.Parent.Parent) {
				return this.Parent;
			}
			this.Parent = this.Parent.GetParent();
			return this.Parent;
		}
	}


    public class Reader
    {
        static StringReader sr;
        public static bool IsDebug = false;
        public static string ReadLine()
        {
            if (IsDebug)
            {
                if (sr == null)
                {
                    sr = new StringReader(InputText.Trim());
                }
                return sr.ReadLine();
            }
            else
            {
                return Console.ReadLine();
            }
        }
        private static string InputText = @"
 
 
 
10 8
5 3 6 8 7 10 9 1 2 4
3 1
4 1
5 9
2 5
6 5
3 5
8 9
7 9 
 
";
    }

    public static void Main(string[] args)
    {
#if DEBUG
        Reader.IsDebug = true;
#endif
        Program prg = new Program();
        prg.Proc();
    }
}

Measured memory_bytes: 40328