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

//using System.Numerics;
using CS=System.Runtime.CompilerServices;
using PL=System.Threading.Tasks.Parallel;
//using System.Diagnostics;
using static System.Math;
using ll=System.Int64;
using static Contest_D.Lib_IO;
using static Contest_D.Lib_Minifunc;


namespace Contest_D {

public static class Entry
{

	public static void Main() {
		checked{
			long n, m;
			Lib_IO.rm(out n, out m);
			long[] p;
			ra(out p);
			P<long,long>[] xy;
			rl(m, out xy);

			DynamicConnectivity uf = new DynamicConnectivity((int)n+1);
			foreach (var e in xy)
			{
				uf.Link((int)e.A,(int)e.B);
			}

			long ans = 0;
			int si = 1;
			bool[] used = new bool[n+1];
			while(si<=n){
				HashSet<int> hs = new HashSet<int>(uf.GetConnectedNodes(si));
				foreach (var f in hs)
				{
					used[f] = true;
					if(0<f&&hs.Contains((int)p[f-1])) ans++;
				}
				for (int i = si+1; i <= n+1; i++)
				{
					if(i==n+1){
						wm(ans);
						return;
					}
					if(!used[i]){
						si = i;
						break;
					}
				}
			}


		}
	}

public class DynamicConnectivity
{
    private int _n;
    private List<DynamicVertex<int>> _vertices;
    private Dictionary<long, DynamicEdge<int>> _edges;

    /// <summary>
    /// Initialize a new instance
    /// </summary>
    /// <param name="n">Number of Vertices</param>
    public DynamicConnectivity(int n)
    {
        _n = n;
        _vertices = new List<DynamicVertex<int>>();
        for (int i = 0; i < n; i++)
        {
            _vertices.Add(DynamicVertex<int>.Create(i));
        }
        _edges = new Dictionary<long, DynamicEdge<int>>();
    }

    /// <summary>
    /// Link an edge between i and j
    /// </summary>
    public void Link(int i, int j)
    {
        if (j > i)
        {
            Swap<int>(ref i, ref j);
        }
        long key = (long)i * _n + j;
        if (_edges.ContainsKey(key))
        {
            throw new ArgumentException("A multiple edge is prohibited");
        }
        var e = _vertices[i].Link(_vertices[j], -1);
        _edges[key] = e;
    }

    /// <summary>
    /// Cut an edge between i and j
    /// </summary>
    public void Cut(int i, int j)
    {
        if (j > i)
        {
            Swap<int>(ref i, ref j);
        }
        long key = (long)i * _n + j;
        DynamicEdge<int> edge;
        if (_edges.TryGetValue(key, out edge))
        {
            edge.Cut();
            _edges.Remove(key);
        }
        else
        {
            throw new ArgumentException("Cutting an edge that does not exist");
        }
    }

    private void Swap<T>(ref T lhs, ref T rhs)
    {
        T temp;
        temp = lhs;
        lhs = rhs;
        rhs = temp;
    }

    /// <summary>
    /// Check if i and j are connected by a path
    /// </summary>
    public bool IsConnected(int i, int j)
    {
        return _vertices[i].IsConnected(_vertices[j]);
    }

    /// <summary>
    /// Connected component size containing i
    /// </summary>
    public int GetConnectedSize(int i)
    {
        int count = _vertices[i].euler[0].node.GetRoot().count;
        // this "count" includes half edges
        // n + (n - 1) * 2 = count
        // n = (count + 2) / 3
        return (count + 2) / 3;
    }

    /// <summary>
    /// Return all the vertices connected to i
    /// </summary>
    public IEnumerable<int> GetConnectedNodes(int i)
    {
        var node = _vertices[i].euler[0].node.GetRoot().leftMost;
        while (node != null)
        {
            int val = node.val.value.value;
            if (val != -1)
            {
                yield return val;
            }
            node = node.next;
        }
    }
}

public class DynamicEdge<T> : DynamicNode<T>
{
    public int key;
    public DynamicVertex<T> source;
    public DynamicVertex<T> dest;
    public int level;
    public List<EulerHalfEdge<DynamicNode<T>>> euler;

    public DynamicEdge(T value, int key, DynamicVertex<T> source, DynamicVertex<T> dest)
    {
        this.value = value;
        this.key = key;
        this.source = source;
        this.dest = dest;
    }

    public void Cut()
    {
        // Don't double cut an edge
        if (source == null)
        {
            return;
        }

        // remove edge from adjacent list of each node
        source.RemoveEdge(this);
        dest.RemoveEdge(this);

        if (euler != null)
        {
            // if euler is not null, the edge is in forests.
            // Cut edge from tree
            for (int i = 0; i < euler.Count; i++)
            {
                euler[i].Cut();
            }

            // Find replacement, looping over levels
            for (int i = level; i >= 0; i--)
            {
                var tv = source.euler[i].node.GetRoot();
                var tw = dest.euler[i].node.GetRoot();
                if (tv.count > tw.count)
                {
                    RaiseLevelOfTree(tw, i);
                    if (Visit(tw, i))
                    {
                        break;
                    }
                }
                else
                {
                    RaiseLevelOfTree(tv, i);
                    if (Visit(tv, i))
                    {
                        break;
                    }
                }
            }
        }
        source = null;
        dest = null;
        euler = null;
        level = 32;
    }

    private void RaiseLevelOfTree(TreapNode<EulerNode<DynamicNode<T>>> node, int level)
    {
        if (node == null)
        {
            return;
        }
        var e = node.val.value as DynamicEdge<T>;
        if (e != null && e.level == level) // second predicate is for avoiding duplicate raise
        {
            e.RaiseLevel();
        }
        RaiseLevelOfTree(node.left, level);
        RaiseLevelOfTree(node.right, level);
    }

    /// <summary>
    /// Search over Tv for edge connecting to Tw
    /// </summary>
    private bool Visit(TreapNode<EulerNode<DynamicNode<T>>> node, int level)
    {
        if (node.flag)
        {
            var v = node.val.value as DynamicVertex<T>;
            var adj = v.adjacent;
            int ptr = EdgeListUtility.LevelIndex(adj, level);
            for (; ptr < adj.Count && adj[ptr].level == level; ptr++)
            {
                var e = adj[ptr];
                var es = e.source;
                var et = e.dest;

                if (es.euler[level].IsConnected(et.euler[level]))
                {
                    // Raise edge level if dest is in current tree
                    e.RaiseLevel();
                    ptr--;
                }
                else
                {
                    // Otherwise, connect trees and finish, because dest is in target tree
                    e.LinkSpanningForests();
                    return true;
                }
            }
        }

        if (node.left != null && node.left.flagAggregate)
        {
            if (Visit(node.left, level))
            {
                return true;
            }
        }
        if (node.right != null && node.right.flagAggregate)
        {
            if (Visit(node.right, level))
            {
                return true;
            }
        }
        return false;
    }

    /// <summary>
    /// Raise the level of the edge, optionally inserting into higher level trees
    /// </summary>
    public void RaiseLevel()
    {
        // Update position in edge lists
        source.RemoveEdge(this);
        dest.RemoveEdge(this);
        level++;
        EdgeListUtility.InsertEdge(source.adjacent, this);
        EdgeListUtility.InsertEdge(dest.adjacent, this);

        // Update flags for s
        if (source.euler.Count <= level)
        {
            source.euler.Add(EulerVertex<DynamicNode<T>>.Create(source));
        }
        var es = source.euler[level];
        es.SetFlag(true);

        // Update flags for t
        if (dest.euler.Count <= level)
        {
            dest.euler.Add(EulerVertex<DynamicNode<T>>.Create(dest));
        }
        var et = dest.euler[level];
        et.SetFlag(true);

        // If the edge is already part of a spanning tree, relink them in the next level
        if (euler != null)
        {
            euler.Add(es.Link(et, this));
        }
    }

    /// <summary>
    /// Add an edge to all spanning forests with level <= this.level
    /// </summary>
    public void LinkSpanningForests()
    {
        var es = source.euler;
        var et = dest.euler;
        euler = new List<EulerHalfEdge<DynamicNode<T>>>();
        for (int i = 0; i <= level; i++)
        {
            if (es.Count <= i)
            {
                es.Add(EulerVertex<DynamicNode<T>>.Create(source));
            }
            if (et.Count <= i)
            {
                et.Add(EulerVertex<DynamicNode<T>>.Create(dest));
            }
            euler.Add(es[i].Link(et[i], this));
        }
    }

    public override string ToString()
    {
        return string.Format("{0}[{1}]:{2}", value, level, key);
    }

    public int CompareTo(DynamicEdge<T> other)
    {
        if (level != other.level)
        {
            return level - other.level;
        }
        return key - other.key;
    }
}

public class DynamicNode<T>
{
    public T value;

    public override string ToString()
    {
        return string.Format("[DN:{0}]", value);
    }
}

public class DynamicVertex<T> : DynamicNode<T>
{
    private static int KeyCounter;

    public List<EulerVertex<DynamicNode<T>>> euler;
    public List<DynamicEdge<T>> adjacent;

    public bool IsConnected(DynamicVertex<T> other)
    {
        return euler[0].IsConnected(other.euler[0]);
    }

    public DynamicEdge<T> Link(DynamicVertex<T> other, T value)
    {
        var e = new DynamicEdge<T>(value, KeyCounter, this, other);
        KeyCounter++;
        if (!euler[0].IsConnected(other.euler[0]))
        {
            e.LinkSpanningForests();
        }
        euler[0].SetFlag(true);
        other.euler[0].SetFlag(true);
        EdgeListUtility.InsertEdge(adjacent, e);
        EdgeListUtility.InsertEdge(other.adjacent, e);
        return e;
    }

    /// <summary>
    /// Remove edge from adjacent list
    /// </summary>
    public void RemoveEdge(DynamicEdge<T> edge)
    {
        var adj = adjacent;
        int idx = EdgeListUtility.Index(adj, edge);
        adj.RemoveAt(idx);
        // Check if flag needs to be updated
        if (!((idx < adj.Count && adj[idx].level == edge.level) ||
            (idx > 0 && adj[idx - 1].level == edge.level)))
        {
            euler[edge.level].SetFlag(false);
        }
    }

    public static DynamicVertex<T> Create(T value)
    {
        var v = new DynamicVertex<T>()
        {
            value = value,
            adjacent = new List<DynamicEdge<T>>()
        };
        var euler = new List<EulerVertex<DynamicNode<T>>>();
        euler.Add(EulerVertex<DynamicNode<T>>.Create(v));
        v.euler = euler;
        return v;
    }
}

public static class EdgeListUtility
{
    private static int GetUpperBound<T>(List<DynamicEdge<T>> list, DynamicEdge<T> edge)
    {
        int low = -1;
        int high = list.Count;
        while (low + 1 < high)
        {
            int mid = (low + high) >> 1;
            int comp = list[mid].CompareTo(edge);
            if (comp > 0)
            {
                high = mid;
            }
            else
            {
                low = mid;
            }
        }
        return high;
    }

    public static void InsertEdge<T>(List<DynamicEdge<T>> list, DynamicEdge<T> edge)
    {
        list.Insert(GetUpperBound(list, edge), edge);
    }

    public static int Index<T>(List<DynamicEdge<T>> list, DynamicEdge<T> edge)
    {
        int idx = GetUpperBound(list, edge);
        return idx - 1;
    }

    public static void RemoveEdge<T>(List<DynamicEdge<T>> list, DynamicEdge<T> edge)
    {
        list.RemoveAt(Index(list, edge));
    }

    public static int LevelIndex<T>(List<DynamicEdge<T>> list, int level)
    {
        int low = -1;
        int high = list.Count;
        while (low + 1 < high)
        {
            int mid = (low + high) >> 1;
            if (list[mid].level >= level)
            {
                high = mid;
            }
            else
            {
                low = mid;
            }
        }
        return high;
    }
}

public class EulerHalfEdge<T> : EulerNode<T>
{
    public EulerVertex<T> source;
    public EulerVertex<T> dest;

    public EulerHalfEdge<T> opposite;

    public EulerHalfEdge(T value, EulerVertex<T> source, EulerVertex<T> dest)
    {
        this.value = value;
        this.source = source;
        this.dest = dest;
    }

    public void CleanUp()
    {
        node.Remove();
        node.val = null;
        node = null;
        opposite = null;
        source = null;
        dest = null;
    }

    public void Cut()
    {
        // Split into parts
        TreapNode<EulerNode<T>> a, b, c, d;
        node.Split(out a, out b);
        opposite.node.Split(out c, out d);

        if (b != null && (b.GetRoot() == c || b.GetRoot() == d))
        {
            // [a, bc, d] or [a, c, bd]
            var res = a.rightMost.Remove();
            if (res != null)
            {
                res.Merge(d);
            }
        }
        else
        {
            // [c, ad, b] or [ca, d, b]
            var res = c.rightMost.Remove();
            if (res != null)
            {
                res.Merge(b);
            }
        }

        opposite.CleanUp();
        CleanUp();
    }
}

public class EulerNode<T>
{
    public T value;
    public TreapNode<EulerNode<T>> node;

    public override string ToString()
    {
        return string.Format("{0}", value);
    }
}

public class EulerVertex<T> : EulerNode<T>
{
    public void SetFlag(bool flag)
    {
        node.SetFlag(flag);
    }

    public bool IsConnected(EulerVertex<T> other)
    {
        return node.GetRoot() == other.node.GetRoot();
    }

    public void MakeRoot()
    {
        TreapNode<EulerNode<T>> left, right;
        node.Split(out left, out right);
        if (right != null)
        {
            right.Merge(left);
        }
    }

    public EulerHalfEdge<T> Link(EulerVertex<T> other, T value)
    {
        // Move both vertices to root
        MakeRoot();
        other.MakeRoot();

        // Create half edges and link them to each other
        var st = new EulerHalfEdge<T>(value, this, other);
        var ts = new EulerHalfEdge<T>(value, other, this);
        ts.opposite = st;
        st.opposite = ts;

        // Insert entries in Euler tours
        st.node = node.Insert(st);
        ts.node = other.node.Insert(ts);

        // Link tours together
        node.Merge(other.node);

        // Return half edge
        return st;
    }

    public int GetCount()
    {
        return node.GetRoot().count;
    }

    public void CleanUp()
    {
        node.Remove();
        node.val = null;
        node = null;
    }

    public static EulerVertex<T> Create(T value)
    {
        var v = new EulerVertex<T>()
        {
            value = value
        };
        v.node = new TreapNode<EulerNode<T>>(v);
        return v;
    }
}

public class TreapNode<T>
{
    public T val;
    public bool flag;
    public bool flagAggregate;
    public TreapNode<T> left;
    public TreapNode<T> right;
    public TreapNode<T> leftMost;
    public TreapNode<T> rightMost;
    public TreapNode<T> prev;
    public TreapNode<T> next;

    public uint priority;
    public int count;
    public TreapNode<T> parent;

    public TreapNode() : this(default(T))
    {
    }

    public TreapNode(T val)
    {
        this.val = val;
        priority = XorShift.Get();
        leftMost = this;
        rightMost = this;
        count = 1;
    }

    private int GetCount(TreapNode<T> node)
    {
        return node != null ? node.count : 0;
    }

    public TreapNode<T> GetRoot()
    {
        var node = this;
        while (node.parent != null)
        {
            node = node.parent;
        }
        return node;
    }

    TreapNode<T> Update()
    {
        count = 1;
        flagAggregate = flag;
        if (left != null)
        {
            count += left.count;
            flagAggregate |= left.flagAggregate;
        }
        if (right != null)
        {
            count += right.count;
            flagAggregate |= right.flagAggregate;
        }
        return this;
    }

    public void SetFlag(bool flag)
    {
        this.flag = flag;
        int counter = 0;
        for (var node = this; node != null; node = node.parent)
        {
            counter++;
            var previous = node.flagAggregate;
            node.Update();
            if (previous == node.flagAggregate)
            {
                break;
            }
        }
    }

    TreapNode<T> LeftLink(TreapNode<T> child)
    {
        if (child == null)
        {
            left = null;
            leftMost = this;
            prev = null;
        }
        else
        {
            left = child;
            child.parent = this;
            prev = child.rightMost;
            child.rightMost.next = this;
            leftMost = child.leftMost;
        }
        return Update();
    }

    TreapNode<T> RightLink(TreapNode<T> child)
    {
        if (child == null)
        {
            right = null;
            rightMost = this;
            next = null;
        }
        else
        {
            right = child;
            child.parent = this;
            next = child.leftMost;
            child.leftMost.prev = this;
            rightMost = child.rightMost;
        }
        return Update();
    }

    public static TreapNode<T> Merge(TreapNode<T> left, TreapNode<T> right)
    {
        if (left == null || right == null)
        {
            return left ?? right;
        }
        if (left.priority > right.priority)
        {
            return left.RightLink(Merge(left.right, right));
        }
        return right.LeftLink(Merge(left, right.left));
    }

    public TreapNode<T> Merge(TreapNode<T> right)
    {
        var left = this;
        if (left != null)
        {
            left = left.GetRoot();
        }
        if (right != null)
        {
            right = right.GetRoot();
        }
        return Merge(left, right);
    }

    /// <summary>
    /// Cut the left child
    /// </summary>
    private void LeftCut()
    {
        if (left != null)
        {
            left.parent = null;
            left.rightMost.next = null;
            prev = left.leftMost.prev;
            left = null;
            leftMost = this;
            prev = null;
            Update();
        }
    }

    /// <summary>
    /// Cut the right child
    /// </summary>
    private void RightCut()
    {
        if (right != null)
        {
            right.parent = null;
            right.leftMost.prev = null;
            next = right.rightMost.next;
            right = null;
            rightMost = this;
            Update();
        }
    }

    public void Split(out TreapNode<T> left, out TreapNode<T> right)
    {
        var node = this;
        right = node.right;
        // Cut node and right
        RightCut();
        left = node;
        // Climb up
        while (node.parent != null)
        {
            var par = node.parent;
            if (par.left == node)
            {
                // Climb to upper right
                par.LeftCut();
                right = par.LeftLink(right);
            }
            else
            {
                // Climb to upper left
                par.RightCut();
                left = par.RightLink(left);
            }
            node = par;
        }
    }

    public TreapNode<T> Insert(T v)
    {
        TreapNode<T> splitLeft, splitRight;
        Split(out splitLeft, out splitRight);
        var node = new TreapNode<T>(v);
        Merge(Merge(splitLeft, node), splitRight);
        return node;
    }

    public TreapNode<T> Remove()
    {
        TreapNode<T> splitLeft, splitRight;
        Split(out splitLeft, out splitRight);
        if (splitLeft.count == 1)
        {
            return splitRight;
        }
        TreapNode<T> removedLeft, thisNode;
        splitLeft.rightMost.prev.Split(out removedLeft, out thisNode);
        return Merge(removedLeft, splitRight);
    }

    public TreapNode<T> Find(T query)
    {
        if (val.Equals(query))
        {
            return this;
        }
        if (left != null)
        {
            var res = left.Find(query);
            if (res != null)
            {
                return res;
            }
        }
        if (right != null)
        {
            var res = right.Find(query);
            if (res != null)
            {
                return res;
            }
        }
        return null;
    }

    public override string ToString()
    {
        string result = val.ToString();
        return result;
    }
}

public static class XorShift
{
    static uint x = 123456789, y = 362436069, z = 521288629, w = 88675123;

    public static uint Get()
    {
        uint t = (x ^ (x << 11));
        x = y;
        y = z;
        z = w;
        return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
    }
}














}



#region BaseModule


public static class Lib_Minifunc{
	public const long MOD1 = 1000000007; // 10^9+7
	public const double EPS1 = 1e-8;
	public const long INF1 = 1000000000000000;  // 10^15
	[CS.MethodImpl(256)] public static void swap1<T>(ref T a,ref T b) { T t = a; a = b; b = t; }
	[CS.MethodImpl(256)] public static void swap2<T>(ref T a,ref T b) where T : IComparable { if (a.CompareTo(b)==1) swap1(ref a, ref b); }  // b の方が小さければ交換

	[CS.MethodImpl(256)] public static bool chmin<T>(ref T a,T b) where T : IComparable { bool r; if (r = (a.CompareTo(b)== 1)) a = b; return r; }  // b の方が小さければ a を更新
	[CS.MethodImpl(256)] public static bool chmax<T>(ref T a,T b) where T : IComparable { bool r; if (r = (a.CompareTo(b)==-1)) a = b; return r; }  // b の方が大きければ a を更新
	[CS.MethodImpl(256)] public static bool inside(long i,long n) => (0<=i&&i<n);
	[CS.MethodImpl(256)] public static bool inside(long x,long y,long w,long h) => (inside(x,w)&&inside(y,h));
	[CS.MethodImpl(256)] public static T min<T>(params T[] a) where T : IComparable { T m = a[0]; for (int i=1;i<a.Length;i++) chmin(ref m, a[i]); return m; }
	[CS.MethodImpl(256)] public static T max<T>(params T[] a) where T : IComparable { T m = a[0]; for (int i=1;i<a.Length;i++) chmax(ref m, a[i]); return m; }
	[CS.MethodImpl(256)] public static long mod(long a,long m=MOD1) { var v = a%m; return (v<0?(m<0?v-m:v+m):v); }
	[CS.MethodImpl(256)] public static long ceil(long a,long b) => (a%b==0||(a<0&&0<b)||(0<a&&b<0)?a/b:a/b+1);	// 整数商の切り上げ
	[CS.MethodImpl(256)] public static long round(double a) => Convert.ToInt64(a);	// 四捨五入
	public static string YES(bool a) => a?"YES":"NO";
	public static string yes(bool a) => a?"Yes":"No";
	[CS.MethodImpl(256)] public static P<T,U> initp<T,U>(T a,U b) => new P<T,U>(a,b);
	[CS.MethodImpl(256)] public static P<T,U,V> initp<T,U,V>(T a,U b,V c) => new P<T,U,V>(a,b,c);
	[CS.MethodImpl(256)] public static P<T,U,V,W> initp<T,U,V,W>(T a,U b,V c,W d) => new P<T,U,V,W>(a,b,c,d);
	public static T[] initc<T>(long l1) where T : new() { var m = new T[l1]; PL.For(0, m.Length, i => m[i] = new T() ); return m; }	// new制約は遅い
	public static int[] range(long s, long c) => Enumerable.Range((int)s,(int)c).ToArray();
	[CS.MethodImpl(256)] public static T[] inita<T>(long l1,T v=default(T)) => Enumerable.Repeat(v,(int)l1).ToArray();
	public static T[][] inita<T>(long l1,long l2,T v=default(T)) { var m = new T[l1][]; PL.For(0, m.Length, i => m[i] = inita(l2,v) ); return m; }
	[CS.MethodImpl(256)] public static bool initd<T,U>(this IDictionary<T,U> d, T k, U v=default(U)) { bool f; if(f = !d.ContainsKey(k)) d[k] = v; return f; }
	public static void shift(this IList<int> l,int o) => PL.For(0, l.Count, i => l[i] += o );
	public static void shift(this IList<long> l,long o) => PL.For(0, l.Count, i => l[i] += o );
	public static void shift(this IList<P<int,int>> l,int o1=0,int o2=0) => PL.For(0, l.Count, i => l[i] = new P<int,int>(l[i].A+o1,l[i].B+o2) );
	public static void shift(this IList<P<long,long>> l,long o1=0,long o2=0) => PL.For(0, l.Count, i => l[i] = new P<long,long>(l[i].A+o1,l[i].B+o2) );
	public static void shift(this IList<P<int,int,int>> l,int o1=0,int o2=0,int o3=0) => PL.For(0, l.Count, i => l[i] = new P<int,int,int>(l[i].A+o1,l[i].B+o2,l[i].C+o3) );
	public static void shift(this IList<P<long,long,long>> l,long o1=0,long o2=0,long o3=0) => PL.For(0, l.Count, i => l[i] = new P<long,long,long>(l[i].A+o1,l[i].B+o2,l[i].C+o3) );
	public static void shift(this IList<P<int,int,int,int>> l,int o1=0,int o2=0,int o3=0,int o4=0) => PL.For(0, l.Count, i => l[i] = new P<int,int,int,int>(l[i].A+o1,l[i].B+o2,l[i].C+o3,l[i].D+o4) );
	public static void shift(this IList<P<long,long,long,long>> l,long o1=0,long o2=0,long o3=0,long o4=0) => PL.For(0, l.Count, i => l[i] = new P<long,long,long,long>(l[i].A+o1,l[i].B+o2,l[i].C+o3,l[i].D+o4) );
	public static KeyValuePair<long,T>[] index<T>(this IList<T> l) { var m = new KeyValuePair<long,T>[l.Count]; PL.For(0, l.Count, i => m[i] = new KeyValuePair<long, T>(i,l[i]) ); return m; }
	public static void sort<T>(this T[] a,int o=1, Comparison<T> f=null) where T : IComparable {
		switch(o){
			case  0: Array.Sort(a,f);break;
			case  1: Array.Sort(a,(x,y)=>x.CompareTo(y));break;
			case -1: Array.Sort(a,(x,y)=>y.CompareTo(x));break;
			default: break;
		}
	}
	public static void sort<T,U>(this P<T,U>[] a,int o, Comparison<P<T,U>> f=null) where T : IComparable where U : IComparable {
		switch(o){
			case  0: Array.Sort(a,f);break;
			case  1: Array.Sort(a,(x,y)=>x.A.CompareTo(y.A));break;
			case -1: Array.Sort(a,(x,y)=>y.A.CompareTo(x.A));break;
			case  2: Array.Sort(a,(x,y)=>x.B.CompareTo(y.B));break;
			case -2: Array.Sort(a,(x,y)=>y.B.CompareTo(x.B));break;
			default: break;
		}
	}
	public static void sort<T,U,V>(this P<T,U,V>[] a,int o, Comparison<P<T,U,V>> f=null) where T : IComparable where U : IComparable where V : IComparable {
		switch(o){
			case  0: Array.Sort(a,f);break;
			case  1: Array.Sort(a,(x,y)=>x.A.CompareTo(y.A));break;
			case -1: Array.Sort(a,(x,y)=>y.A.CompareTo(x.A));break;
			case  2: Array.Sort(a,(x,y)=>x.B.CompareTo(y.B));break;
			case -2: Array.Sort(a,(x,y)=>y.B.CompareTo(x.B));break;
			case  3: Array.Sort(a,(x,y)=>x.C.CompareTo(y.C));break;
			case -3: Array.Sort(a,(x,y)=>y.C.CompareTo(x.C));break;
			default: break;
		}
	}
	public static void sort<T,U,V,W>(this P<T,U,V,W>[] a,int o, Comparison<P<T,U,V,W>> f=null) where T : IComparable where U : IComparable where V : IComparable where W : IComparable {
		switch(o){
			case  0: Array.Sort(a,f);break;
			case  1: Array.Sort(a,(x,y)=>x.A.CompareTo(y.A));break;
			case -1: Array.Sort(a,(x,y)=>y.A.CompareTo(x.A));break;
			case  2: Array.Sort(a,(x,y)=>x.B.CompareTo(y.B));break;
			case -2: Array.Sort(a,(x,y)=>y.B.CompareTo(x.B));break;
			case  3: Array.Sort(a,(x,y)=>x.C.CompareTo(y.C));break;
			case -3: Array.Sort(a,(x,y)=>y.C.CompareTo(x.C));break;
			case  4: Array.Sort(a,(x,y)=>x.D.CompareTo(y.D));break;
			case -4: Array.Sort(a,(x,y)=>y.D.CompareTo(x.D));break;
			default: break;
		}
	}
}


public struct P<T,U> {
	public T A; public U B;
	public P(T a,U b) { A = a; B = b; }
	public static implicit operator KeyValuePair<T,U>(P<T,U> a) => new KeyValuePair<T,U>(a.A,a.B);
	public static implicit operator P<T,U>(KeyValuePair<T,U> a) => new P<T,U>(a.Key,a.Value);
}
public struct P<T,U,V> {
	public T A; public U B; public V C;
	public P(T a,U b,V c) { A = a; B = b; C = c; }
}
public struct P<T,U,V,W> {
	public T A; public U B; public V C; public W D;
	public P(T a,U b,V c,W d) { A = a; B = b; C = c; D = d; }
}

public static class Lib_IO {
	class Prt : System.IO.StreamWriter {
		public override IFormatProvider FormatProvider { get { return System.Globalization.CultureInfo.InvariantCulture; } }
		public Prt(System.IO.Stream s) : base(s,new UTF8Encoding(false,true)) {}
		public Prt(System.IO.Stream s,Encoding e) : base(s,e) {}
	}
	static Prt sw = new Prt(Console.OpenStandardOutput());
	static char[] sp = new char[] {' '};
	[CS.MethodImpl(256)] static bool eq<T,U>() => typeof(T).Equals(typeof(U));
	[CS.MethodImpl(256)] static T ct<T,U>(U a) => (T)Convert.ChangeType(a,typeof(T));
	[CS.MethodImpl(256)] static T cv<T>(string s) =>
		eq<T,int>()	? ct<T,int>(int.Parse(s))
	  : eq<T,long>()   ? ct<T,long>(long.Parse(s))
	  : eq<T,double>() ? ct<T,double>(double.Parse(s,System.Globalization.CultureInfo.InvariantCulture))
	  : eq<T,char>()   ? ct<T,char>(s[0])
					   : ct<T,string>(s);
	public static string[] rm<T>(out T a) {
		var z = Console.ReadLine().Split(sp,StringSplitOptions.RemoveEmptyEntries); 
		a = cv<T>(z[0]);
		return z;
	}
	public static string[] rm<T,U>(out T a,out U b) {
		var z = rm<T>(out a); 
		b = cv<U>(z[1]);
		return z;
	}
	public static string[] rm<T,U,V>(out T a,out U b,out V c) {
		var z = rm<T,U>(out a,out b); 
		c = cv<V>(z[2]);
		return z;
	}
	public static string[] rm<T,U,V,W>(out T a,out U b,out V c,out W d) {
		var z = rm<T,U,V>(out a,out b,out c); 
		d = cv<W>(z[3]);
		return z;
	}
	public static string[] rm<T,U,V,W,X>(out T a,out U b,out V c,out W d,out X e) {
		var z = rm<T,U,V,W>(out a,out b,out c,out d); 
		e = cv<X>(z[4]);
		return z;
	}
	public static string[] rm<T,U,V,W,X,Y>(out T a,out U b,out V c,out W d,out X e,out Y f) {
		var z = rm<T,U,V,W,X>(out a,out b,out c,out d,out e); 
		f = cv<Y>(z[5]);
		return z;
	}
	public static string[] ra<T>(out T[] a) {		
		var z = Console.ReadLine().Split(sp,StringSplitOptions.RemoveEmptyEntries);
		a = new T[z.Length];
		for(int i=0;i<a.Length;i++) a[i] = cv<T>(z[i]);
		return z;
	}
	public static string[][] rl<T>(long n,out T[] a) {
		a = new T[n];
		var y = new string[n][];
		for(int i=0;i<n;i++) y[i] = rm(out a[i]);
		return y;
	}
	public static string[][] rl<T,U>(long n,out P<T,U>[] a) {
		a = new P<T,U>[n];
		var y = new string[n][];
		for(int i=0;i<n;i++) {
			T o; U p;
			y[i] = rm(out o,out p);
			a[i] = new P<T,U>(o,p);
		}
		return y;
	}
	public static string[][] rl<T,U,V>(long n,out P<T,U,V>[] a) {
		a = new P<T,U,V>[n];
		var y = new string[n][];
		for(int i=0;i<n;i++) {
			T o; U p; V q;
			y[i] = rm(out o,out p,out q);
			a[i] = new P<T,U,V>(o,p,q);
		}
		return y;
	}
	public static string[][] rl<T,U,V,W>(long n,out P<T,U,V,W>[] a) {
		a = new P<T,U,V,W>[n];
		var y = new string[n][];
		for(int i=0;i<n;i++) {
			T o; U p; V q; W r;
			y[i] = rm(out o,out p,out q,out r);
			a[i] = new P<T,U,V,W>(o,p,q,r);
		}
		return y;
	}
	public static string[][] rx<T>(long n,out T[][] a) {
		a = new T[n][];
		var y = new string[n][];
		for(int i=0;i<n;i++) y[i] = ra(out a[i]);
		return y;
	}

	static void wwp(Action act){
		sw.AutoFlush = false;
		Console.SetOut(sw);
		act();
		Console.Out.Flush();
		sw.AutoFlush = true;
		Console.SetOut(sw);
	}
	[CS.MethodImpl(256)] static string wfm(Type t) =>t.Equals(typeof(double)) ? "{0:F10}" : "{0}";
	public static void wm(params object[] a) {
		wwp(()=>{
			for(int i=0;i<a.Length-1;i++) Console.Write(wfm(a[i].GetType())+" ",a[i]);
			Console.WriteLine(wfm(a[a.Length-1].GetType()),a[a.Length-1]);
		});
	}
	public static void wa<T>(IList<T> a) { wxa(new IList<T>[]{a}, " "); }
	public static void wl<T>(IList<T> a) {
		wwp(()=>{
			var f = wfm(typeof(T));
			if(a.Count==0){
				Console.WriteLine();
			}else{
				foreach(T x in a) Console.WriteLine(f,x);
			}
		});
	}
	static void wxa<T>(IList<IList<T>> a, string s) {
		wwp(()=>{
			var f = wfm(typeof(T));
			var g = f + s;
			foreach(var b in a) {
				if(b.Count==0){
					Console.WriteLine();
				}else{
					for(int i=0;i<b.Count-1;i++) Console.Write(g,b[i]);
					Console.WriteLine(f,b[b.Count-1]);
				}
			}
		});
	}
	public static void wx<T>(IList<IList<T>> a) { wxa(a, " "); }
	public static void wg<T>(IList<IList<T>> a) { wxa(a, ""); }
}

#endregion



}

Measured memory_bytes: 123212