public static void Run()
        {
            using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
            using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
            {
                int n = fs.NextInt(), m = fs.NextInt();
                int[] p = new int[n];
                DisjointSet dsu = new DisjointSet();
                for (int i = 0; i < n; i++)
                {
                    p[i] = fs.NextInt() - 1;
                    dsu.MakeSet(i);
                }
                for (int i = 0; i < m; i++)
                {
                    int u = fs.NextInt() - 1, v = fs.NextInt() - 1;
                    dsu.Union(p[u], p[v]);
                }

                LinkedList<int>[] lists = new LinkedList<int>[n];
                for (int i = n - 1; i >= 0; i--)
                {
                    int set = dsu.FindSet(i);
                    if (lists[set] == null) lists[set] = new LinkedList<int>();
                    lists[set].AddLast(i);
                }
                for (int i = 0; i < n; i++)
                {
                    int set = dsu.FindSet(p[i]);
                    writer.Write(lists[set].First() + 1 + " ");
                    lists[set].RemoveFirst();
                }
            }
        }
 public static void Run()
 {
     using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
     using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
     {
         int n = fs.NextInt();
         DisjointSet dsu = new DisjointSet();
         List<string> cycles = new List<string>();
         for (int i = 1; i <= n; i++) 
         {
             dsu.MakeSet(i);
         }
         for (int i = 0; i < n - 1; i++)
         {
             int a = fs.NextInt(), b = fs.NextInt();
             if (dsu.FindSet(a) == dsu.FindSet(b)) cycles.Add(a + " " + b);
             else dsu.Union(a, b);
         }
         HashSet<int> hashSet = new HashSet<int>();
         for (int i = 1; i <= n; i++)
         {
             hashSet.Add(dsu.FindSet(i));
         }
         int[] leaders = hashSet.ToArray();
         writer.WriteLine(leaders.Length - 1);
         int j = 0;
         for (int i = 0; i < leaders.Length - 1; i++)
         {
             writer.WriteLine(cycles[j++] + " " + leaders[i] + " " + leaders[i + 1]);
         }
     }
 }