import java.io.*;
import java.util.*;

import static java.lang.Math.*;
import static java.util.Arrays.*;

public class CF342A {
    static BufferedReader __in;
    static PrintWriter __out;
    static StringTokenizer input;

    public static void main(String[] args) throws IOException {
        __in = new BufferedReader(new InputStreamReader(System.in));
        __out = new PrintWriter(new OutputStreamWriter(System.out));

        int n = ri();
        int[] a = new int[8];
        r();
        while(n --> 0) a[ni()]++;
        if(a[5] == 0 && a[7] == 0 && a[1] == a[4] + a[6] && a[2] + a[3] == a[4] + a[6]) {
            for(int i = 0; i < a[4]; ++i) {
                prln("1 2 4"); --a[2];
            }
            for(int i = 0; i < a[2]; ++i) {
                prln("1 2 6");
            }
            for(int i = 0; i < a[3]; ++i) {
                prln("1 3 6");
            }
        } else {
            prln(-1);
        }

        close();
    }

    // references
    // IBIG = 1e9 + 7
    // IRAND ~= 3e8
    // IMAX ~= 2e10
    // LMAX ~= 9e18
    // constants
    static final int IBIG = 1000000007;
    static final int IRAND = 327859546;
    static final int IMAX = 2147483647;
    static final int IMIN = -2147483648;
    static final long LMAX = 9223372036854775807L;
    static final long LMIN = -9223372036854775808L;
    // util
    static int minof(int a, int b, int c) {return min(a, min(b, c));}
    static int minof(int... x) {return x.length == 1 ? x[0] : x.length == 2 ? min(x[0], x[1]) : min(x[0], minstarting(1, x));}
    static int minstarting(int offset, int... x) {assert x.length > 2; return offset == x.length - 2 ? min(x[offset], x[offset + 1]) : min(x[offset], minstarting(offset + 1, x));}
    static long minof(long a, long b, long c) {return min(a, min(b, c));}
    static long minof(long... x) {return x.length == 1 ? x[0] : x.length == 2 ? min(x[0], x[1]) : min(x[0], minstarting(1, x));}
    static long minstarting(int offset, long... x) {assert x.length > 2; return offset == x.length - 2 ? min(x[offset], x[offset + 1]) : min(x[offset], minstarting(offset + 1, x));}
    static int maxof(int a, int b, int c) {return max(a, max(b, c));}
    static int maxof(int... x) {return x.length == 1 ? x[0] : x.length == 2 ? max(x[0], x[1]) : max(x[0], maxstarting(1, x));}
    static int maxstarting(int offset, int... x) {assert x.length > 2; return offset == x.length - 2 ? max(x[offset], x[offset + 1]) : max(x[offset], maxstarting(offset + 1, x));}
    static long maxof(long a, long b, long c) {return max(a, max(b, c));}
    static long maxof(long... x) {return x.length == 1 ? x[0] : x.length == 2 ? max(x[0], x[1]) : max(x[0], maxstarting(1, x));}
    static long maxstarting(int offset, long... x) {assert x.length > 2; return offset == x.length - 2 ? max(x[offset], x[offset + 1]) : max(x[offset], maxstarting(offset + 1, x));}
    static int powi(int a, int b) {if(a == 0) return 0; int ans = 1; while(b > 0) {if((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
    static long powl(long a, int b) {if(a == 0) return 0; long ans = 1; while(b > 0) {if((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
    static int floori(double d) {return (int)d;}
    static int ceili(double d) {return (int)ceil(d);}
    static long floorl(double d) {return (long)d;}
    static long ceill(double d) {return (long)ceil(d);}
    // input
    static void r() throws IOException {input = new StringTokenizer(__in.readLine());}
    static int ri() throws IOException {return Integer.parseInt(__in.readLine());}
    static long rl() throws IOException {return Long.parseLong(__in.readLine());}
    static int[] ria(int n) throws IOException {int[] a = new int[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Integer.parseInt(input.nextToken()); return a;}
    static long[] rla(int n) throws IOException {long[] a = new long[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Long.parseLong(input.nextToken()); return a;}
    static char[] rcha() throws IOException {return __in.readLine().toCharArray();}
    static String rline() throws IOException {return __in.readLine();}
    static int rni() throws IOException {input = new StringTokenizer(__in.readLine()); return Integer.parseInt(input.nextToken());}
    static int ni() {return Integer.parseInt(input.nextToken());}
    static long rnl() throws IOException {input = new StringTokenizer(__in.readLine()); return Long.parseLong(input.nextToken());}
    static long nl() {return Long.parseLong(input.nextToken());}
    // output
    static void pr(int i) {__out.print(i);}
    static void prln(int i) {__out.println(i);}
    static void pr(long l) {__out.print(l);}
    static void prln(long l) {__out.println(l);}
    static void pr(double d) {__out.print(d);}
    static void prln(double d) {__out.println(d);}
    static void pr(char c) {__out.print(c);}
    static void prln(char c) {__out.println(c);}
    static void pr(char[] s) {__out.print(new String(s));}
    static void prln(char[] s) {__out.println(new String(s));}
    static void pr(String s) {__out.print(s);}
    static void prln(String s) {__out.println(s);}
    static void pr(Object o) {__out.print(o);}
    static void prln(Object o) {__out.println(o);}
    static void prln() {__out.println();}
    static void pryes() {__out.println("yes");}
    static void pry() {__out.println("Yes");}
    static void prY() {__out.println("YES");}
    static void prno() {__out.println("no");}
    static void prn() {__out.println("No");}
    static void prN() {__out.println("NO");}
    static void pryesno(boolean b) {__out.println(b ? "yes" : "no");};
    static void pryn(boolean b) {__out.println(b ? "Yes" : "No");}
    static void prYN(boolean b) {__out.println(b ? "YES" : "NO");}
    static void prln(int... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);}
    static void prln(long... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);}
    static <T> void prln(Collection<T> c) {int n = c.size() - 1; Iterator<T> iter = c.iterator(); for(int i = 0; i < n; __out.print(iter.next()), __out.print(' '), ++i); __out.println(iter.next());}
    static void flush() {__out.flush();}
    static void close() {__out.close();}}


import java.io.*;
import java.util.*;

import static java.lang.Math.*;
import static java.util.Arrays.*;

public class CF342A {
    static BufferedReader __in;
    static PrintWriter __out;
    static StringTokenizer input;

    public static void main(String[] args) throws IOException {
        __in = new BufferedReader(new InputStreamReader(System.in));
        __out = new PrintWriter(new OutputStreamWriter(System.out));

        int n = ri();
        int[] a = new int[8];
        r();
        while(n --> 0) a[ni()]++;
        if(a[5] == 0 && a[7] == 0 && a[1] == a[4] + a[6] && a[2] + a[3] == a[4] + a[6] && a[2] >= a[4]) {
            for(int i = 0; i < a[4]; ++i) {
                prln("1 2 4"); --a[2];
            }
            for(int i = 0; i < a[2]; ++i) {
                prln("1 2 6");
            }
            for(int i = 0; i < a[3]; ++i) {
                prln("1 3 6");
            }
        } else {
            prln(-1);
        }

        close();
    }

    // references
    // IBIG = 1e9 + 7
    // IRAND ~= 3e8
    // IMAX ~= 2e10
    // LMAX ~= 9e18
    // constants
    static final int IBIG = 1000000007;
    static final int IRAND = 327859546;
    static final int IMAX = 2147483647;
    static final int IMIN = -2147483648;
    static final long LMAX = 9223372036854775807L;
    static final long LMIN = -9223372036854775808L;
    // util
    static int minof(int a, int b, int c) {return min(a, min(b, c));}
    static int minof(int... x) {return x.length == 1 ? x[0] : x.length == 2 ? min(x[0], x[1]) : min(x[0], minstarting(1, x));}
    static int minstarting(int offset, int... x) {assert x.length > 2; return offset == x.length - 2 ? min(x[offset], x[offset + 1]) : min(x[offset], minstarting(offset + 1, x));}
    static long minof(long a, long b, long c) {return min(a, min(b, c));}
    static long minof(long... x) {return x.length == 1 ? x[0] : x.length == 2 ? min(x[0], x[1]) : min(x[0], minstarting(1, x));}
    static long minstarting(int offset, long... x) {assert x.length > 2; return offset == x.length - 2 ? min(x[offset], x[offset + 1]) : min(x[offset], minstarting(offset + 1, x));}
    static int maxof(int a, int b, int c) {return max(a, max(b, c));}
    static int maxof(int... x) {return x.length == 1 ? x[0] : x.length == 2 ? max(x[0], x[1]) : max(x[0], maxstarting(1, x));}
    static int maxstarting(int offset, int... x) {assert x.length > 2; return offset == x.length - 2 ? max(x[offset], x[offset + 1]) : max(x[offset], maxstarting(offset + 1, x));}
    static long maxof(long a, long b, long c) {return max(a, max(b, c));}
    static long maxof(long... x) {return x.length == 1 ? x[0] : x.length == 2 ? max(x[0], x[1]) : max(x[0], maxstarting(1, x));}
    static long maxstarting(int offset, long... x) {assert x.length > 2; return offset == x.length - 2 ? max(x[offset], x[offset + 1]) : max(x[offset], maxstarting(offset + 1, x));}
    static int powi(int a, int b) {if(a == 0) return 0; int ans = 1; while(b > 0) {if((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
    static long powl(long a, int b) {if(a == 0) return 0; long ans = 1; while(b > 0) {if((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
    static int floori(double d) {return (int)d;}
    static int ceili(double d) {return (int)ceil(d);}
    static long floorl(double d) {return (long)d;}
    static long ceill(double d) {return (long)ceil(d);}
    // input
    static void r() throws IOException {input = new StringTokenizer(__in.readLine());}
    static int ri() throws IOException {return Integer.parseInt(__in.readLine());}
    static long rl() throws IOException {return Long.parseLong(__in.readLine());}
    static int[] ria(int n) throws IOException {int[] a = new int[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Integer.parseInt(input.nextToken()); return a;}
    static long[] rla(int n) throws IOException {long[] a = new long[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Long.parseLong(input.nextToken()); return a;}
    static char[] rcha() throws IOException {return __in.readLine().toCharArray();}
    static String rline() throws IOException {return __in.readLine();}
    static int rni() throws IOException {input = new StringTokenizer(__in.readLine()); return Integer.parseInt(input.nextToken());}
    static int ni() {return Integer.parseInt(input.nextToken());}
    static long rnl() throws IOException {input = new StringTokenizer(__in.readLine()); return Long.parseLong(input.nextToken());}
    static long nl() {return Long.parseLong(input.nextToken());}
    // output
    static void pr(int i) {__out.print(i);}
    static void prln(int i) {__out.println(i);}
    static void pr(long l) {__out.print(l);}
    static void prln(long l) {__out.println(l);}
    static void pr(double d) {__out.print(d);}
    static void prln(double d) {__out.println(d);}
    static void pr(char c) {__out.print(c);}
    static void prln(char c) {__out.println(c);}
    static void pr(char[] s) {__out.print(new String(s));}
    static void prln(char[] s) {__out.println(new String(s));}
    static void pr(String s) {__out.print(s);}
    static void prln(String s) {__out.println(s);}
    static void pr(Object o) {__out.print(o);}
    static void prln(Object o) {__out.println(o);}
    static void prln() {__out.println();}
    static void pryes() {__out.println("yes");}
    static void pry() {__out.println("Yes");}
    static void prY() {__out.println("YES");}
    static void prno() {__out.println("no");}
    static void prn() {__out.println("No");}
    static void prN() {__out.println("NO");}
    static void pryesno(boolean b) {__out.println(b ? "yes" : "no");};
    static void pryn(boolean b) {__out.println(b ? "Yes" : "No");}
    static void prYN(boolean b) {__out.println(b ? "YES" : "NO");}
    static void prln(int... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);}
    static void prln(long... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);}
    static <T> void prln(Collection<T> c) {int n = c.size() - 1; Iterator<T> iter = c.iterator(); for(int i = 0; i < n; __out.print(iter.next()), __out.print(' '), ++i); __out.println(iter.next());}
    static void flush() {__out.flush();}
    static void close() {__out.close();}}


import java.io.PrintWriter;
import java.util.*;

public class task_0 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        Scanner s = new Scanner(System.in);
        int n = in.nextInt();
        int data[] = new int[8];
        data[0] = 0;
        for(int i=0;i<n;i++)
        {
            data[in.nextInt()]++;
        }
       // for(int x : data) System.out.print(x);

        int m = n/3;
        if(data[1]!=m||data[5]>0||data[7]>0||data[2]<data[4]||data[6]<data[3] || data[2]+data[3]!=data[4]+data[6]){
            System.out.println(-1);
        }
        else{
            for(int i =1;i<=data[4];i++) System.out.println("1 2 4");
            for(int i =1;i<=data[2]-data[4];i++) System.out.println("1 2 6");
            for(int i =1;i<=data[3];i++) System.out.println("1 3 6");
        }
    }

    }


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.Scanner;
import java.util.ArrayList;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author grolegor
 */
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);
        TaskA solver = new TaskA();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskA {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int n = in.nextInt();
            int[] a = new int[8];
            for (int i = 0; i < n; i++) {
                int num = in.nextInt();
                a[num]++;
            }
            if (a[5] != 0 || a[7] != 0) {
                out.println(-1);
                return;
            }

            if (a[6] == 0 && a[4] == 0) {
                out.println(-1);
                return;
            }

            List<String> ans = new ArrayList<>();
            while (a[6] != 0) {
                if (a[3] > 0 && a[1] > 0) {
                    a[3]--;
                    a[1]--;
                    ans.add(1 + " " + 3 + " " + 6);
                } else if (a[2] > 0 && a[1] > 0) {
                    a[2]--;
                    a[1]--;
                    ans.add(1 + " " + 2 + " " + 6);
                } else {
                    out.println(-1);
                    return;
                }
                a[6]--;
            }
            while (a[4] != 0) {
                if (a[2] > 0 && a[1] > 0) {
                    a[2]--;
                    a[1]--;
                    ans.add(1 + " " + 2 + " " + 4);
                }
                a[4]--;
            }
            if (a[3] != 0 || a[2] != 0 || a[1] != 0) {
                out.println(-1);
            } else {
                for (String s : ans) {
                    out.println(s);
                }
            }
        }

    }
}


import java.io.*;
import java.util.*;

public class Main {
    private static void solver(InputReader sc, PrintWriter out) {
            int n = sc.nextInt();
            int arr[] = new int[8];
            boolean flag=  false;
            int a =0,b=0,c=0;
            for(int i=0;i<n;i++){
                int  x = sc.nextInt();
                if(x==5 || x==7){
                    flag = true;
                }
               arr[x]++;
            }
            if(flag){
                out.println(-1);
            }else{
                while(true) {
                    if (arr[1] > 0 && arr[2] > 0 && arr[4] > 0) {
                        a++;
                        arr[1]--;
                        arr[2]--;
                        arr[4]--;
                    }else if (arr[1] > 0 && arr[2] > 0 && arr[6] > 0) {
                            b++;
                            arr[1]--;
                            arr[2]--;
                            arr[6]--;
                        } else if (arr[1] > 0 && arr[3] > 0 && arr[6] > 0) {
                            c++;
                            arr[1]--;
                            arr[3]--;
                            arr[6]--;
                        }
                        else
                            break;
                }
                for(int i=0;i<8;i++){
                    if (arr[i] > 0) {
                        flag = true;
                        break;
                    }
                }
                if(!flag){
                    while(a-- > 0){
                        out.println("1 "+"2 "+"4");
                    }
                    while (b-- > 0)
                        out.println("1 "+"2 "+"6 ");
                    while (c-- > 0)
                        out.println("1 "+"3 "+"6 ");
                }else
                    out.println(-1);
            }
            }
    private static int gcd (int a, int b) {
        if (b == 0)
            return a;
        else
            return gcd (b, a % b);
    }
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        solver(in, out);
        out.close();

    }
    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }
        public long nextLong() {
            return Long.parseLong(next());
        }
    }
}


import java.io.*;
import java.util.*;

public class Ladder {
    public static void main(String[] args) throws IOException {
        //BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        //Scanner scanner=new Scanner(System.in);
        PrintWriter pw=new PrintWriter(System.out);
        Reader scanner = new Reader();
        int n=scanner.nextInt();
        int[] seq=new int[8];
        //boolean flag=false;

        for (int i = 0; i <n ; i++) {
            int t=scanner.nextInt();
            seq[t]++;

        }
        //System.out.println(Arrays.toString(seq));
        if (seq[5]!=0 || seq[7]!=0){
            pw.println(-1);
            pw.close();
            return;
        }
            if (seq[1]==seq[2] && seq[2]==(seq[4]+seq[6])){
                for (int i = 0; i <seq[4] ; i++) {
                    pw.println("1 2 4");
                }
                for (int i = 0; i <seq[6] ; i++) {
                    pw.println("1 2 6");
                }
            }
            else if (seq[1]==(seq[3]+seq[2]) && seq[3]<=seq[6] && (seq[2]+seq[3])==(seq[4]+seq[6])){
                for (int i = 0; i <seq[3] ; i++) {
                    pw.println("1 3 6");
                }
                for (int i = 0; i <seq[6]-seq[3] ; i++) {
                    pw.println("1 2 6");
                }
                for (int i = 0; i <seq[4] ; i++) {
                    pw.println("1 2 4");
                }
            }

        else{
            pw.println(-1);
        }

        pw.close();

    }
}
/*
class tuple implements Comparable<tuple>{
    int a;
    int b;

    public tuple(int a, int b) {
        this.a = a;
        this.b = b;
    }

    @Override
    public int compareTo(tuple o) {
        return this.a-o.a;
    }
}

 */
class Reader {
    final private int BUFFER_SIZE = 1 << 16;
    private DataInputStream din;
    private byte[] buffer;
    private int bufferPointer, bytesRead;

    public Reader() {
        din = new DataInputStream(System.in);
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public Reader(String file_name) throws IOException {
        din = new DataInputStream(new FileInputStream(file_name));
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public String readLine() throws IOException {
        byte[] buf = new byte[64];
        int cnt = 0, c;
        while ((c = read()) != -1) {
            if (c == '\n') break;
            buf[cnt++] = (byte) c;
        }
        return new String(buf, 0, cnt);
    }

    public int nextInt() throws IOException {
        int ret = 0;
        byte c = read();
        while (c <= ' ') c = read();
        boolean neg = (c == '-');
        if (neg) c = read();
        do {
            ret = ret * 10 + c - '0';
        } while ((c = read()) >= '0' && c <= '9');
        if (neg) return -ret;
        return ret;
    }

    public long nextLong() throws IOException {
        long ret = 0;
        byte c = read();
        while (c <= ' ') c = read();
        boolean neg = (c == '-');
        if (neg) c = read();
        do {
            ret = ret * 10 + c - '0';
        } while ((c = read()) >= '0' && c <= '9');
        if (neg) return -ret;
        return ret;
    }

    public double nextDouble() throws IOException {
        double ret = 0, div = 1;
        byte c = read();
        while (c <= ' ') c = read();
        boolean neg = (c == '-');
        if (neg) c = read();
        do {
            ret = ret * 10 + c - '0';
        } while ((c = read()) >= '0' && c <= '9');
        if (c == '.') while ((c = read()) >= '0' && c <= '9') ret += (c - '0') / (div *= 10);
        if (neg) return -ret;
        return ret;
    }

    private void fillBuffer() throws IOException {
        bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
        if (bytesRead == -1) buffer[0] = -1;
    }

    private byte read() throws IOException {
        if (bufferPointer == bytesRead) fillBuffer();
        return buffer[bufferPointer++];
    }

    public void close() throws IOException {
        if (din == null) return;
        din.close();
    }
}


import java.io.*;
import java.util.*;

public class Ladder {
    public static void main(String[] args) throws IOException {
        //BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        //Scanner scanner=new Scanner(System.in);
        PrintWriter pw=new PrintWriter(System.out);
        Reader scanner = new Reader();
        int n=scanner.nextInt();
        int[] seq=new int[8];

        for (int i = 0; i <n ; i++) {
            int t=scanner.nextInt();
            seq[t]++;

        }
        if (seq[1]==seq[2] && seq[2]==(seq[4]+seq[6])){
            for (int i = 0; i <seq[4] ; i++) {
                pw.println("1 2 4");
            }
            for (int i = 0; i <seq[6] ; i++) {
                pw.println("1 2 6");
            }
        }
        else if (seq[1]==(seq[3]+seq[2]) && seq[3]<=seq[6] && (seq[2]+seq[3])==(seq[4]+seq[6])){
            for (int i = 0; i <seq[3] ; i++) {
                pw.println("1 3 6");
            }
            for (int i = 0; i <seq[6]-seq[3] ; i++) {
                pw.println("1 2 6");
            }
            for (int i = 0; i <seq[4] ; i++) {
                pw.println("1 2 4");
            }
        }
        else{
            pw.println(-1);
        }

        pw.close();

    }
}
/*
class tuple implements Comparable<tuple>{
    int a;
    int b;

    public tuple(int a, int b) {
        this.a = a;
        this.b = b;
    }

    @Override
    public int compareTo(tuple o) {
        return this.a-o.a;
    }
}

 */
class Reader {
    final private int BUFFER_SIZE = 1 << 16;
    private DataInputStream din;
    private byte[] buffer;
    private int bufferPointer, bytesRead;

    public Reader() {
        din = new DataInputStream(System.in);
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public Reader(String file_name) throws IOException {
        din = new DataInputStream(new FileInputStream(file_name));
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public String readLine() throws IOException {
        byte[] buf = new byte[64];
        int cnt = 0, c;
        while ((c = read()) != -1) {
            if (c == '\n') break;
            buf[cnt++] = (byte) c;
        }
        return new String(buf, 0, cnt);
    }

    public int nextInt() throws IOException {
        int ret = 0;
        byte c = read();
        while (c <= ' ') c = read();
        boolean neg = (c == '-');
        if (neg) c = read();
        do {
            ret = ret * 10 + c - '0';
        } while ((c = read()) >= '0' && c <= '9');
        if (neg) return -ret;
        return ret;
    }

    public long nextLong() throws IOException {
        long ret = 0;
        byte c = read();
        while (c <= ' ') c = read();
        boolean neg = (c == '-');
        if (neg) c = read();
        do {
            ret = ret * 10 + c - '0';
        } while ((c = read()) >= '0' && c <= '9');
        if (neg) return -ret;
        return ret;
    }

    public double nextDouble() throws IOException {
        double ret = 0, div = 1;
        byte c = read();
        while (c <= ' ') c = read();
        boolean neg = (c == '-');
        if (neg) c = read();
        do {
            ret = ret * 10 + c - '0';
        } while ((c = read()) >= '0' && c <= '9');
        if (c == '.') while ((c = read()) >= '0' && c <= '9') ret += (c - '0') / (div *= 10);
        if (neg) return -ret;
        return ret;
    }

    private void fillBuffer() throws IOException {
        bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
        if (bytesRead == -1) buffer[0] = -1;
    }

    private byte read() throws IOException {
        if (bufferPointer == bytesRead) fillBuffer();
        return buffer[bufferPointer++];
    }

    public void close() throws IOException {
        if (din == null) return;
        din.close();
    }
}


import java.io.*;
import java.util.*;

public class Ladder {
    public static void main(String[] args) throws IOException {
        //BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        //Scanner scanner=new Scanner(System.in);
        PrintWriter pw=new PrintWriter(System.out);
        Reader scanner = new Reader();
        int n=scanner.nextInt();
        int[] seq=new int[8];
        //boolean flag=false;

        for (int i = 0; i <n ; i++) {
            int t=scanner.nextInt();
            seq[t]++;

        }
        //System.out.println(Arrays.toString(seq));
        if (seq[5]!=0 || seq[7]!=0){
            pw.println(-1);
            return;
        }
            if (seq[1]==seq[2] && seq[2]==(seq[4]+seq[6])){
                for (int i = 0; i <seq[4] ; i++) {
                    pw.println("1 2 4");
                }
                for (int i = 0; i <seq[6] ; i++) {
                    pw.println("1 2 6");
                }
            }
            else if (seq[1]==(seq[3]+seq[2]) && seq[3]<=seq[6] && (seq[2]+seq[3])==(seq[4]+seq[6])){
                for (int i = 0; i <seq[3] ; i++) {
                    pw.println("1 3 6");
                }
                for (int i = 0; i <seq[6]-seq[3] ; i++) {
                    pw.println("1 2 6");
                }
                for (int i = 0; i <seq[4] ; i++) {
                    pw.println("1 2 4");
                }
            }

        else{
            pw.println(-1);
        }

        pw.close();

    }
}
/*
class tuple implements Comparable<tuple>{
    int a;
    int b;

    public tuple(int a, int b) {
        this.a = a;
        this.b = b;
    }

    @Override
    public int compareTo(tuple o) {
        return this.a-o.a;
    }
}

 */
class Reader {
    final private int BUFFER_SIZE = 1 << 16;
    private DataInputStream din;
    private byte[] buffer;
    private int bufferPointer, bytesRead;

    public Reader() {
        din = new DataInputStream(System.in);
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public Reader(String file_name) throws IOException {
        din = new DataInputStream(new FileInputStream(file_name));
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public String readLine() throws IOException {
        byte[] buf = new byte[64];
        int cnt = 0, c;
        while ((c = read()) != -1) {
            if (c == '\n') break;
            buf[cnt++] = (byte) c;
        }
        return new String(buf, 0, cnt);
    }

    public int nextInt() throws IOException {
        int ret = 0;
        byte c = read();
        while (c <= ' ') c = read();
        boolean neg = (c == '-');
        if (neg) c = read();
        do {
            ret = ret * 10 + c - '0';
        } while ((c = read()) >= '0' && c <= '9');
        if (neg) return -ret;
        return ret;
    }

    public long nextLong() throws IOException {
        long ret = 0;
        byte c = read();
        while (c <= ' ') c = read();
        boolean neg = (c == '-');
        if (neg) c = read();
        do {
            ret = ret * 10 + c - '0';
        } while ((c = read()) >= '0' && c <= '9');
        if (neg) return -ret;
        return ret;
    }

    public double nextDouble() throws IOException {
        double ret = 0, div = 1;
        byte c = read();
        while (c <= ' ') c = read();
        boolean neg = (c == '-');
        if (neg) c = read();
        do {
            ret = ret * 10 + c - '0';
        } while ((c = read()) >= '0' && c <= '9');
        if (c == '.') while ((c = read()) >= '0' && c <= '9') ret += (c - '0') / (div *= 10);
        if (neg) return -ret;
        return ret;
    }

    private void fillBuffer() throws IOException {
        bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
        if (bytesRead == -1) buffer[0] = -1;
    }

    private byte read() throws IOException {
        if (bufferPointer == bytesRead) fillBuffer();
        return buffer[bufferPointer++];
    }

    public void close() throws IOException {
        if (din == null) return;
        din.close();
    }
}


import java.io.*;
import java.util.*;

public class Ladder {
    public static void main(String[] args) throws IOException {
        //BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        //Scanner scanner=new Scanner(System.in);
        PrintWriter pw=new PrintWriter(System.out);
        Reader scanner = new Reader();
        int n=scanner.nextInt();
        int[] seq=new int[8];
        //boolean flag=false;

        for (int i = 0; i <n ; i++) {
            int t=scanner.nextInt();
            seq[t]++;

        }
        //System.out.println(Arrays.toString(seq));
        if (seq[1]!=0 || seq[2]!=0 || seq[3]!=0 || seq[6]!=0){
            if (seq[1]==seq[2] && seq[2]==(seq[4]+seq[6])){
                for (int i = 0; i <seq[4] ; i++) {
                    pw.println("1 2 4");
                }
                for (int i = 0; i <seq[6] ; i++) {
                    pw.println("1 2 6");
                }
            }
            else if (seq[1]==(seq[3]+seq[2]) && seq[3]<=seq[6] && (seq[2]+seq[3])==(seq[4]+seq[6])){
                for (int i = 0; i <seq[3] ; i++) {
                    pw.println("1 3 6");
                }
                for (int i = 0; i <seq[6]-seq[3] ; i++) {
                    pw.println("1 2 6");
                }
                for (int i = 0; i <seq[4] ; i++) {
                    pw.println("1 2 4");
                }
            }
        }
        else{
            pw.println(-1);
        }

        pw.close();

    }
}
/*
class tuple implements Comparable<tuple>{
    int a;
    int b;

    public tuple(int a, int b) {
        this.a = a;
        this.b = b;
    }

    @Override
    public int compareTo(tuple o) {
        return this.a-o.a;
    }
}

 */
class Reader {
    final private int BUFFER_SIZE = 1 << 16;
    private DataInputStream din;
    private byte[] buffer;
    private int bufferPointer, bytesRead;

    public Reader() {
        din = new DataInputStream(System.in);
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public Reader(String file_name) throws IOException {
        din = new DataInputStream(new FileInputStream(file_name));
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public String readLine() throws IOException {
        byte[] buf = new byte[64];
        int cnt = 0, c;
        while ((c = read()) != -1) {
            if (c == '\n') break;
            buf[cnt++] = (byte) c;
        }
        return new String(buf, 0, cnt);
    }

    public int nextInt() throws IOException {
        int ret = 0;
        byte c = read();
        while (c <= ' ') c = read();
        boolean neg = (c == '-');
        if (neg) c = read();
        do {
            ret = ret * 10 + c - '0';
        } while ((c = read()) >= '0' && c <= '9');
        if (neg) return -ret;
        return ret;
    }

    public long nextLong() throws IOException {
        long ret = 0;
        byte c = read();
        while (c <= ' ') c = read();
        boolean neg = (c == '-');
        if (neg) c = read();
        do {
            ret = ret * 10 + c - '0';
        } while ((c = read()) >= '0' && c <= '9');
        if (neg) return -ret;
        return ret;
    }

    public double nextDouble() throws IOException {
        double ret = 0, div = 1;
        byte c = read();
        while (c <= ' ') c = read();
        boolean neg = (c == '-');
        if (neg) c = read();
        do {
            ret = ret * 10 + c - '0';
        } while ((c = read()) >= '0' && c <= '9');
        if (c == '.') while ((c = read()) >= '0' && c <= '9') ret += (c - '0') / (div *= 10);
        if (neg) return -ret;
        return ret;
    }

    private void fillBuffer() throws IOException {
        bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
        if (bytesRead == -1) buffer[0] = -1;
    }

    private byte read() throws IOException {
        if (bufferPointer == bytesRead) fillBuffer();
        return buffer[bufferPointer++];
    }

    public void close() throws IOException {
        if (din == null) return;
        din.close();
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
	public static void main(String[] args) {
		FastReader reader = new FastReader();
		int n = reader.nextInt();
		int[] a = reader.readArray(n);
		reader.close();
		int[] f = new int[7];
		for (int i : a)
			++f[i - 1];
		// 1 2 4
		// 1 2 6
		// 1 3 6
		StringBuilder sb = new StringBuilder("");
		while (f[0] > 0) {
			if (f[1] > 0 && f[3] > 0) {
				sb = sb.append("1 2 4\n");
				--f[0];
				--f[1];
				--f[3];
			} else if (f[1] > 0 && f[5] > 0) {
				sb = sb.append("1 2 6\n");
				--f[0];
				--f[1];
				--f[5];
			} else if (f[2] > 0 && f[5] > 0) {
				sb = sb.append("1 3 6\n");
				--f[0];
				--f[2];
				--f[5];
			} else {
				System.out.println(-1);
				return;
			}
		}
		System.out.println(sb.toString().isEmpty() ? -1 : sb.toString().trim());
	}

	static class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine().trim());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine().trim();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}

		int[] readArray(int size) {
			int[] ar = new int[size];
			int i = 0;
			try {
				for (String s : br.readLine().trim().split("\\s+"))
					ar[i++] = Integer.parseInt(s);
			} catch (IOException e) {
				e.printStackTrace();
			}
			return ar;
		}

		void close() {
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
	public static void main(String[] args) {
		FastReader reader = new FastReader();
		int n = reader.nextInt();
		int[] a = reader.readArray(n);
		reader.close();
		int[] f = new int[7];
		for (int i : a)
			++f[i - 1];
		// 1 2 4
		// 1 2 6
		// 1 3 6
		StringBuilder sb = new StringBuilder("");
		while (f[0] > 0) {
			if (f[1] > 0 && f[3] > 0) {
				sb = sb.append("1 2 4\n");
				--f[0];
				--f[1];
				--f[3];
			} else if (f[1] > 0 && f[5] > 0) {
				sb = sb.append("1 2 6\n");
				--f[0];
				--f[1];
				--f[5];
			} else if (f[2] > 0 && f[5] > 0) {
				sb = sb.append("1 3 6\n");
				--f[0];
				--f[2];
				--f[5];
			} else {
				System.out.println(-1);
				return;
			}
		}
		System.out.println(sb.isEmpty() ? -1 : sb.toString().trim());
	}

	static class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine().trim());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine().trim();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}

		int[] readArray(int size) {
			int[] ar = new int[size];
			int i = 0;
			try {
				for (String s : br.readLine().trim().split("\\s+"))
					ar[i++] = Integer.parseInt(s);
			} catch (IOException e) {
				e.printStackTrace();
			}
			return ar;
		}

		void close() {
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
	public static void main(String[] args) {
		FastReader reader = new FastReader();
		int n = reader.nextInt();
		int[] a = reader.readArray(n);
		reader.close();
		int[] f = new int[7];
		for (int i : a)
			++f[i - 1];
		// 1 2 4
		// 1 2 6
		// 1 3 6
		StringBuilder sb = new StringBuilder("");
		while (f[0] > 0) {
			if (f[1] > 0 && f[3] > 0) {
				sb = sb.append("1 2 4\n");
				--f[0];
				--f[1];
				--f[3];
			} else if (f[1] > 0 && f[5] > 0) {
				sb = sb.append("1 2 6\n");
				--f[0];
				--f[1];
				--f[5];
			} else if (f[2] > 0 && f[5] > 0) {
				sb = sb.append("1 3 6\n");
				--f[0];
				--f[2];
				--f[5];
			} else {
				System.out.println(-1);
				return;
			}
		}
		System.out.println(sb.toString().trim());
	}

	static class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine().trim());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine().trim();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}

		int[] readArray(int size) {
			int[] ar = new int[size];
			int i = 0;
			try {
				for (String s : br.readLine().trim().split("\\s+"))
					ar[i++] = Integer.parseInt(s);
			} catch (IOException e) {
				e.printStackTrace();
			}
			return ar;
		}

		void close() {
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
	public static void main(String[] args) {
		FastReader reader = new FastReader();
		int n = reader.nextInt();
		int[] a = reader.readArray(n);
		reader.close();
		int[] f = new int[7];
		for (int i : a)
			++f[i - 1];
		// 1 2 4
		// 1 2 6
		// 1 3 6
		StringBuilder sb = new StringBuilder("");
		while (f[0] > 0) {
			if (f[1] > 0 && f[3] > 0) {
				sb = sb.append("1 2 4\n");
				--f[0]; --f[1]; --f[3];
			} else if (f[1] > 0 && f[5] > 0) {
				sb = sb.append("1 2 6\n");
				--f[0]; --f[1]; --f[5];
			} else if (f[2] > 0 && f[5] > 0) {
				sb = sb.append("1 3 6\n");
				--f[0]; --f[2]; --f[5];
			} else {
				System.out.println(-1);
				return;
			}
		}
		for (int i : f)
			if (i > 0) {
				System.out.println(-1);
				return;
			}
		System.out.println(sb.toString().trim());
	}

	static class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine().trim());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine().trim();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}

		int[] readArray(int size) {
			int[] ar = new int[size];
			int i = 0;
			try {
				for (String s : br.readLine().trim().split("\\s+"))
					ar[i++] = Integer.parseInt(s);
			} catch (IOException e) {
				e.printStackTrace();
			}
			return ar;
		}

		void close() {
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
	public static void main(String[] args) {
		FastReader reader = new FastReader();
		int n = reader.nextInt();
		int[] a = reader.readArray(n);
		reader.close();
		int[] f = new int[7];
		for (int i : a)
			++f[i - 1];
		// 1 2 4
		// 1 2 6
		// 1 3 6
		StringBuilder sb = new StringBuilder("");
		while (f[0] > 0) {
			if (f[1] > 0 && f[3] > 0) {
				sb = sb.append("1 2 4\n");
				--f[1]; --f[3];
			} else if (f[1] > 0 && f[5] > 0) {
				sb = sb.append("1 2 6\n");
				--f[1]; --f[5];
			} else if (f[2] > 0 && f[5] > 0) {
				sb = sb.append("1 3 6\n");
				--f[2]; --f[5];
			} else {
				System.out.println(-1);
				return;
			}
			--f[0];
		}
		for (int i : f)
			if (i > 0) {
				System.out.println(-1);
				return;
			}
		System.out.println(sb.toString().trim());
	}

	static class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine().trim());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine().trim();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}

		int[] readArray(int size) {
			int[] ar = new int[size];
			int i = 0;
			try {
				for (String s : br.readLine().trim().split("\\s+"))
					ar[i++] = Integer.parseInt(s);
			} catch (IOException e) {
				e.printStackTrace();
			}
			return ar;
		}

		void close() {
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
	public static void main(String[] args) {
		FastReader reader = new FastReader();
		int n = reader.nextInt();
		int[] a = reader.readArray(n);
		reader.close();
		int[] f = new int[7];
		for (int i : a)
			++f[i - 1];
		// 1 2 4
		// 1 2 6
		// 1 3 6
		StringBuilder sb = new StringBuilder("");
		while (f[0] > 0) {
			if (f[1] > 0 && f[3] > 0) {
				sb = sb.append("1 2 4\n");
				--f[0];
				--f[1];
				--f[3];
			} else if (f[1] > 0 && f[5] > 0) {
				sb = sb.append("1 2 6\n");
				--f[0];
				--f[1];
				--f[5];
			} else if (f[2] > 0 && f[5] > 0) {
				sb = sb.append("1 3 6\n");
				--f[0];
				--f[2];
				--f[5];
			} else {
				System.out.println(-1);
				return;
			}
		}
		for (int i : f)
			if (i > 0) {
				System.out.println(-1);
				return;
			}
		System.out.println(sb.toString().trim());
	}

	static class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine().trim());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine().trim();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}

		int[] readArray(int size) {
			int[] ar = new int[size];
			int i = 0;
			try {
				for (String s : br.readLine().trim().split("\\s+"))
					ar[i++] = Integer.parseInt(s);
			} catch (IOException e) {
				e.printStackTrace();
			}
			return ar;
		}

		void close() {
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Divisors {
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(bf.readLine());
        int [][] ans ={{1,2,4},{1,2,6},{1,3,6},};
        int [] count = new int [7];
        StringTokenizer st = new StringTokenizer(bf.readLine());
        for (int i = 0; i < n; i++) {
            int num = Integer.parseInt(st.nextToken());
            count[num]++;
        }
        int [] sol = {0,0,0};
        for (int i = 0; i < n/3; i++) {
            for (int num:ans[0]) {
                if (count[num]>0) {
                    count[num]--;
                    sol[0]++;
                }
            }

            for (int num:ans[1]) {
                if (count[num]>0) {
                    count[num]--;
                    sol[1]++;
                }
            }
            for (int num:ans[2]) {
                if (count[num]>0) {
                    count[num]--;
                    sol[2]++;
                }
            }
        }
        int sum=0;
        for (int num :
                count) {
            sum+=num;
        }
        if (sum==0){
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < sol[i]/3; j++) {
                    for (int no:ans[i]) {
                        System.out.print(no+" ");
                    }
                    System.out.println("");
                }
            }
        }
        else
            System.out.println(-1);
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Divisors {
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(bf.readLine());
        int [][] ans ={{1,2,4},{1,2,6},{1,3,6},};
        int [] count = new int [7];
        StringTokenizer st = new StringTokenizer(bf.readLine());
        for (int i = 0; i < n; i++) {
            int num = Integer.parseInt(st.nextToken());
            count[num]++;
        }
        int [] sol = {0,0,0};
        for (int i = 0; i < n/3; i++) {
            if (count[4]>0) {
                for (int num : ans[0]) {
                    count[num]--;
                    sol[0]++;
                }
            }
            if (count[2]>0) {
                for (int num : ans[1]) {
                    count[num]--;
                    sol[1]++;

                }
            }
            if (count[6]>0) {
                for (int num : ans[2]) {

                    count[num]--;
                    sol[2]++;
                }
            }
        }
        int sum=0;
        for (int num :
                count) {
            sum+=num;
        }
        if (sum==0){
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < sol[i]/3; j++) {
                    for (int no:ans[i]) {
                        System.out.print(no+" ");
                    }
                    System.out.println("");
                }
            }
        }
        else
            System.out.println(-1);
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Divisors {
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(bf.readLine());
        int [] count = new int [8];
        StringTokenizer st = new StringTokenizer(bf.readLine());
        for (int i = 0; i < n; i++) {
            int num = Integer.parseInt(st.nextToken());
            count[num]++;
        }
        if (count[1]!=n/3||count[7]>0||count[5]>0||count[2]<count[4]||count[3]>count[6]||count[2]+count[3]!=count[4]+count[6])
            System.out.println(-1);
        else {
            for (int i = 0; i < count[4]; i++) {
                System.out.println("1 2 4");
            }
            for (int i = 0; i < count[2]-count[4]; i++) {
                System.out.println("1 2 6");
            }
            for (int i = 0; i < count[3]; i++) {
                System.out.println("1 3 6");
            }
        }
    }
}


import java.util.Scanner;

public class Main
{

    static void groupOfThree(int [] arr, int size){
        int oneS = 0, twoS = 0, threeS = 0,fourS = 0, sixS = 0;
        for (int i = 0; i < size; i++){
            if (arr[i] == 1)
                oneS++;
            if (arr[i] == 2)
                twoS++;
            if (arr[i] == 3)
                threeS++;
            if (arr[i] == 4)
                fourS++;
            if (arr[i] == 6)
                sixS++;
        }
        boolean isThereASolution = false;
        twoS = twoS - fourS;
        oneS = oneS - fourS;
        oneS = oneS - threeS;
        sixS = sixS - threeS;
        if (oneS == twoS && oneS == sixS){
            while (fourS > 0){
        	   System.out.println("1 2 4");
        	   fourS--;
        	   isThereASolution = true;
            }
            while (twoS > 0 ){
        	   System.out.println("1 2 6");
        	   twoS--;
        	   isThereASolution = true;
            }
            while (threeS > 0 ){
        	   System.out.println("1 3 6");
        	   threeS--;
        	   isThereASolution = true;
            }
            if (!isThereASolution){
        	   System.out.println("-1");
            }
        }else {
    	   System.out.println("-1");
        }
    }

	public static void main(String[] args) {

   	    Scanner in = new Scanner(System.in);
        int size = in.nextInt();
        int [] arr = new int [size];
        for(int i = 0; i < size; i++){
            arr[i] = in.nextInt();
        }
        groupOfThree(arr, size);
	}
}


import java.util.Scanner;

public class Main
{

    static void groupOfThree(int [] arr, int size){
        int oneS = 0, twoS = 0, threeS = 0,fourS = 0, sixS = 0;
        for (int i = 0; i < size; i++){
            if (arr[i] == 1)
                oneS++;
            if (arr[i] == 2)
                twoS++;
            if (arr[i] == 3)
                threeS++;
            if (arr[i] == 4)
                fourS++;
            if (arr[i] == 6)
                sixS++;
        }
        boolean isThereASolution = false;
        while (oneS > 0 && twoS > 0 && fourS > 0){
    	   System.out.println("1 2 4");
    	   oneS--;
    	   twoS--;
    	   fourS--;
    	   isThereASolution = true;
        }
        while (oneS > 0 && twoS > 0 && sixS > 0){
    	   System.out.println("1 2 6");
    	   oneS--;
    	   twoS--;
    	   sixS--;
    	   isThereASolution = true;

        }
        while (oneS > 0 && threeS > 0 && sixS > 0){
    	   System.out.println("1 3 6");
    	   oneS--;
    	   threeS--;
    	   sixS--;
    	   isThereASolution = true;
        }

        if (!isThereASolution){
    	   System.out.println("-1");
        }
    }

	public static void main(String[] args) {

   	    Scanner in = new Scanner(System.in);
        int size = in.nextInt();
        int [] arr = new int [size];
        for(int i = 0; i < size; i++){
            arr[i] = in.nextInt();
        }
        groupOfThree(arr, size);
	}
}


import java.util.Scanner;

public class Main
{

   static void groupOfThree(int [] arr, int size){
        int oneS = 0, twoS = 0, threeS = 0,fourS = 0, sixS = 0;
        for (int i = 0; i < size; i++){
            if (arr[i] == 1)
                oneS++;
            else if (arr[i] == 2)
                twoS++;
            else if (arr[i] == 3)
                threeS++;
            else if (arr[i] == 4)
                fourS++;
            else if (arr[i] == 6)
                sixS++;
            else{
        	   System.out.println("-1");
                return;
            }

        }
        boolean isThereASolution = false;
        twoS = twoS - fourS;
        oneS = oneS - fourS;
        oneS = oneS - threeS;
        sixS = sixS - threeS;
        if (oneS >= 0 && twoS >= 0 && threeS >= 0 && fourS >= 0 && sixS >=0){
            if (oneS == twoS && oneS == sixS){
                while (fourS > 0){
            	   System.out.println("1 2 4");
            	   fourS--;
            	   isThereASolution = true;
                }
                while (twoS > 0 ){
            	   System.out.println("1 2 6");
            	   twoS--;
            	   isThereASolution = true;
                }
                while (threeS > 0 ){
            	   System.out.println("1 3 6");
            	   threeS--;
            	   isThereASolution = true;
                }
                if (!isThereASolution){
            	   System.out.println("-1");
                }
            }else {
        	   System.out.println("-1");

            }
        }else {
    	   System.out.println("-1");
        }
    }

	public static void main(String[] args) {

   	    Scanner in = new Scanner(System.in);
        int size = in.nextInt();
        int [] arr = new int [size];
        for(int i = 0; i < size; i++){
            arr[i] = in.nextInt();
        }
        groupOfThree(arr, size);
	}
}


import java.util.Scanner;

public class Main
{

    static void groupOfThree(int [] arr, int size){
        int oneS = 0, twoS = 0, threeS = 0,fourS = 0, sixS = 0;
        for (int i = 0; i < size; i++){
            if (arr[i] == 1)
                oneS++;
            if (arr[i] == 2)
                twoS++;
            if (arr[i] == 3)
                threeS++;
            if (arr[i] == 4)
                fourS++;
            if (arr[i] == 6)
                sixS++;
        }
        boolean isThereASolution = false;
        twoS = twoS - fourS;
        oneS = oneS - fourS;
        oneS = oneS - threeS;
        sixS = sixS - threeS;
        if (oneS >= 0 && twoS >= 0 && threeS >= 0 && fourS >= 0 && sixS >=0){
            if (oneS == twoS && oneS == sixS){
                while (fourS > 0){
            	   System.out.println("1 2 4");
            	   fourS--;
            	   isThereASolution = true;
                }
                while (twoS > 0 ){
            	   System.out.println("1 2 6");
            	   twoS--;
            	   isThereASolution = true;
                }
                while (threeS > 0 ){
            	   System.out.println("1 3 6");
            	   threeS--;
            	   isThereASolution = true;
                }
                if (!isThereASolution){
            	   System.out.println("-1");
                }
            }else {
        	   System.out.println("-1");

            }
        }else {
    	   System.out.println("-1");
        }
    }

	public static void main(String[] args) {

   	    Scanner in = new Scanner(System.in);
        int size = in.nextInt();
        int [] arr = new int [size];
        for(int i = 0; i < size; i++){
            arr[i] = in.nextInt();
        }
        groupOfThree(arr, size);
	}
}


import java.util.Scanner;

public class Main
{

    static void groupOfThree(int [] arr, int size){
        int oneS = 0, twoS = 0, threeS = 0,fourS = 0, sixS = 0;
        for (int i = 0; i < size; i++){
            if (arr[i] == 1)
                oneS++;
            if (arr[i] == 2)
                twoS++;
            if (arr[i] == 3)
                threeS++;
            if (arr[i] == 4)
                fourS++;
            if (arr[i] == 6)
                sixS++;
        }
        twoS = twoS - fourS;
        oneS = oneS - fourS;
        oneS = oneS - threeS;
        sixS = sixS - threeS;
        if (oneS == twoS && oneS == sixS){
            while (fourS > 0){
        	   System.out.println("1 2 4");
        	   fourS--;
            }
            while (twoS > 0 ){
        	   System.out.println("1 2 6");
        	   twoS--;
            }
            while (threeS > 0 ){
        	   System.out.println("1 3 6");
        	   threeS--;
            }
        }else {
    	   System.out.println("-1");
        }
    }

	public static void main(String[] args) {

   	    Scanner in = new Scanner(System.in);
        int size = in.nextInt();
        int [] arr = new int [size];
        for(int i = 0; i < size; i++){
            arr[i] = in.nextInt();
        }
        groupOfThree(arr, size);
	}
}


import java.util.Scanner;

public class Main
{
    static void groupOfThree(int [] arr, int size){
        boolean isOneExists = false, isTwoExists = false, isThreeExists = false,isFourExists = false, isSixExists = false;
        for (int i = 0; i < size; i++){
            if (arr[i] == 1)
                isOneExists = true;
            if (arr[i] == 2)
                isTwoExists = true;
            if (arr[i] == 3)
                isThreeExists = true;
            if (arr[i] == 4)
                isFourExists = true;
            if (arr[i] == 6)
                isSixExists = true;
        }
        boolean isThereASolution = false;
        if (isOneExists && isTwoExists && isFourExists){
    	   System.out.println("1 2 4");
    	   isThereASolution = true;
        }
        if (isOneExists && isTwoExists && isSixExists){
    	   System.out.println("1 2 6");
    	   isThereASolution = true;

        }
        if (isOneExists && isThreeExists && isSixExists){
    	   System.out.println("1 3 6");
    	   isThereASolution = true;
        }

        if (!isThereASolution){
    	   System.out.println("-1");
        }
    }

	public static void main(String[] args) {

   	    Scanner in = new Scanner(System.in);
        int size = in.nextInt();
        int [] arr = new int [size];
        for(int i = 0; i < size; i++){
            arr[i] = in.nextInt();
        }
        groupOfThree(arr, size);
	}
}


import java.util.*;
import java.io.*;
			public class yahia {
				public static void main (String [] Yahia_Mostafa) throws Exception {
					Scanner sc =new Scanner(System.in);
					PrintWriter pw =new PrintWriter(System.out);
					int n =sc.nextInt();
					int [] coun =new int [7];
					for (int i=0;i<n;++i)
						coun[sc.nextInt()-1]++;
					if (coun[4]!=0||coun[6]!=0||(coun[0]*2!=coun[1]+coun[3]+coun[5])) {
						pw.print(-1);
						pw.close();
						return;
					}
					StringBuilder sb =new StringBuilder();
					while (coun[3]-->0) {
						if (coun[1]==0)
							break;
						sb.append("1 2 4 ");
						coun[0]--;
						coun[1]--;
					}
					coun[3]++;
					while (coun[1]-->0) {
						if (coun[5]==0)
							break;
						sb.append("1 2 6 ");
						coun[5]--;
						coun[0]--;
					}
					coun[1]++;
					while (coun[2]-->0) {
						if (coun[5]==0)
							break;
						sb.append("1 3 6 ");
						coun[5]--;
						coun[0]--;
					}
					coun[2]++;
					for (int i=0;i<7;++i)
						if (coun[i]!=0)
						{
							System.out.println(-1);
							return;
						}
					pw.print(sb);
					pw.close();
					}
				static class Scanner {
					StringTokenizer stringTokenizer;
					BufferedReader bfBufferedReader;
					public Scanner(InputStream system) {
						bfBufferedReader=new BufferedReader(new InputStreamReader( system));
					}
					 public Scanner(String file) throws Exception {
				            bfBufferedReader = new BufferedReader(new FileReader( file));
				        }
				        public String next() throws IOException {
				            while (stringTokenizer == null || !stringTokenizer.hasMoreTokens())
				                stringTokenizer = new StringTokenizer( bfBufferedReader.readLine());
				            return stringTokenizer.nextToken();
				        }

				        public String nextLine() throws IOException {
				            return bfBufferedReader.readLine();
				        }

				        public int nextInt() throws IOException {
				            return Integer.parseInt(next());
				        }

				        public double nextDouble() throws IOException {
				            return Double.parseDouble(next());
				        }

				        public char nextChar() throws IOException {
				            return next().charAt(0);
				        }

				        public Long nextLong() throws IOException {
				            return Long.parseLong(next());
				        }

				        public boolean ready() throws IOException {
				            return bfBufferedReader.ready();
				        }

				        public void waitForInput() throws InterruptedException {
				            Thread.sleep(3000);
				        }
				    }

				}


import java.util.*;
import java.io.*;
			public class yahia {
				public static void main (String [] Yahia_Mostafa) throws Exception {
					Scanner sc =new Scanner(System.in);
					PrintWriter pw =new PrintWriter(System.out);
					int n =sc.nextInt();
					int [] coun =new int [7];
					for (int i=0;i<n;++i)
						coun[sc.nextInt()-1]++;
					if (coun[4]!=0||coun[6]!=0||(coun[0]*2!=coun[1]+coun[3]+coun[2]+coun[5])) {
						pw.print(-1);
						pw.close();
						return;
					}

					StringBuilder sb =new StringBuilder();
					while (coun[3]-->0) {
						if (coun[1]==0)
							break;
						sb.append("1 2 4 ");
						coun[0]--;
						coun[1]--;
					}
					coun[3]++;
					while (coun[1]-->0) {
						if (coun[5]==0)
							break;
						sb.append("1 2 6 ");
						coun[5]--;
						coun[0]--;
					}
					coun[1]++;
					while (coun[2]-->0) {
						if (coun[5]==0)
							break;
						sb.append("1 3 6 ");
						coun[5]--;
						coun[0]--;
					}
					coun[2]++;
					for (int i=0;i<7;++i)
						if (coun[i]!=0)
						{
							System.out.println(-1);
							return;
						}
					pw.print(sb);
					pw.close();
					}
				static class Scanner {
					StringTokenizer stringTokenizer;
					BufferedReader bfBufferedReader;
					public Scanner(InputStream system) {
						bfBufferedReader=new BufferedReader(new InputStreamReader( system));
					}
					 public Scanner(String file) throws Exception {
				            bfBufferedReader = new BufferedReader(new FileReader( file));
				        }
				        public String next() throws IOException {
				            while (stringTokenizer == null || !stringTokenizer.hasMoreTokens())
				                stringTokenizer = new StringTokenizer( bfBufferedReader.readLine());
				            return stringTokenizer.nextToken();
				        }

				        public String nextLine() throws IOException {
				            return bfBufferedReader.readLine();
				        }

				        public int nextInt() throws IOException {
				            return Integer.parseInt(next());
				        }

				        public double nextDouble() throws IOException {
				            return Double.parseDouble(next());
				        }

				        public char nextChar() throws IOException {
				            return next().charAt(0);
				        }

				        public Long nextLong() throws IOException {
				            return Long.parseLong(next());
				        }

				        public boolean ready() throws IOException {
				            return bfBufferedReader.ready();
				        }

				        public void waitForInput() throws InterruptedException {
				            Thread.sleep(3000);
				        }
				    }

				}


import java.util.*;
import java.io.*;
			public class yahia {
				public static void main (String [] Yahia_Mostafa) throws Exception {
					Scanner sc =new Scanner(System.in);
					PrintWriter pw =new PrintWriter(System.out);
					int n =sc.nextInt();
					int [] coun =new int [7];
					for (int i=0;i<n;++i)
						coun[sc.nextInt()-1]++;
					if (coun[4]!=0||coun[6]!=0||(coun[0]*2!=coun[1]+coun[3]+coun[5]))
						pw.print(-1);
					StringBuilder sb =new StringBuilder();
					while (coun[3]-->0) {
						if (coun[1]==0)
							break;
						sb.append("1 2 4 ");
						coun[0]--;
						coun[1]--;
					}
					coun[3]++;
					while (coun[1]-->0) {
						if (coun[5]==0)
							break;
						sb.append("1 2 6 ");
						coun[5]--;
						coun[0]--;
					}
					coun[1]++;
					while (coun[2]-->0) {
						if (coun[5]==0)
							break;
						sb.append("1 3 6 ");
						coun[5]--;
						coun[0]--;
					}
					coun[2]++;
					for (int i=0;i<n;++i)
						if (coun[i]!=0)
						{
							System.out.println(-1);
							return;
						}
					pw.print(sb);
					pw.close();
					}
				static class Scanner {
					StringTokenizer stringTokenizer;
					BufferedReader bfBufferedReader;
					public Scanner(InputStream system) {
						bfBufferedReader=new BufferedReader(new InputStreamReader( system));
					}
					 public Scanner(String file) throws Exception {
				            bfBufferedReader = new BufferedReader(new FileReader( file));
				        }
				        public String next() throws IOException {
				            while (stringTokenizer == null || !stringTokenizer.hasMoreTokens())
				                stringTokenizer = new StringTokenizer( bfBufferedReader.readLine());
				            return stringTokenizer.nextToken();
				        }

				        public String nextLine() throws IOException {
				            return bfBufferedReader.readLine();
				        }

				        public int nextInt() throws IOException {
				            return Integer.parseInt(next());
				        }

				        public double nextDouble() throws IOException {
				            return Double.parseDouble(next());
				        }

				        public char nextChar() throws IOException {
				            return next().charAt(0);
				        }

				        public Long nextLong() throws IOException {
				            return Long.parseLong(next());
				        }

				        public boolean ready() throws IOException {
				            return bfBufferedReader.ready();
				        }

				        public void waitForInput() throws InterruptedException {
				            Thread.sleep(3000);
				        }
				    }

				}


import java.util.*;
public class Main
{
    public static void main(String arg[])
    {
        Scanner in=new Scanner(System.in);
        int n=in.nextInt();
        int arr[]=new int[n];
        String str[]=new String[n/3];
        for(int i=0;i<n;i++)
        {
            arr[i]=in.nextInt();
        }
        int val1=0;
        int val2=0;
        int val3=0;
        int val4=0;
        int val6=0;
        int flag=0;
        for(int i=0;i<arr.length;i++)
        {
            if(arr[i]==5||arr[i]==7)
            {
                flag=1;
                break;
            }
            else if(arr[i]==1)
            val1++;
            else if(arr[i]==2)
            val2++;
            else if(arr[i]==3)
            val3++;
            else if(arr[i]==4)
            val4++;
            else if(arr[i]==6)
            val6++;
        }
        int x=0;
    int a=n/3;
    if(val1>a)
    flag=1;
    else if(val3>val6||val3> val1)
    flag=1;
for(int y=0;y<a;y++)
{

    for(int i=0;i<val3;i++)
    {
        str[y]=("1 3 6");
        y++;
        x++;
        val1--;
        val3--;
        val6--;
    }
    if(val1>0&&val2>0&&val4>0)
    {
        for(int i=0;i<val4;i++)
        {
           str[y]= ("1 2 4");
           y++;
            x++;
            val1--;
            val2--;
            val4--;
        }
    }
     if(val1>0&&val2>0&&val6>0)
    {
        for(int i=0;i<val6;i++)
        {
            str[y]= ("1 2 6");
            y++;
            x++;
            val1--;
            val2--;
            val6--;
        }
    }
    if(val2>0&&val4>0&&val6>0)
    {
        for(int i=0;i<val4;i++)
        {
            str[y]= ("2 4 6");
            x++;
            val2--;
            val4--;
            val6--;
        }
    }
}
    if(x!=a)
    flag=1;
    if(flag==1)
    System.out.println("-1");
    else
    {
        for(int i=0;i<str.length;i++)
        {
            System.out.println(str[i]);
        }
    }

    }
}


import java.util.*;
public class Main
{
    public static void main(String arg[])
    {
        Scanner in=new Scanner(System.in);
        int n=in.nextInt();
        int arr[]=new int[n];
        String str[]=new String[n/3];
        for(int i=0;i<n;i++)
        {
            arr[i]=in.nextInt();
        }
        int val1=0;
        int val2=0;
        int val3=0;
        int val4=0;
        int val6=0;
        int flag=0;
        for(int i=0;i<n;i++)
        {
            if(arr[i]==5||arr[i]==7)
            {
                flag=1;
                break;
            }
            else if(arr[i]==1)
            val1++;
            else if(arr[i]==2)
            val2++;
            else if(arr[i]==3)
            val3++;
            else if(arr[i]==4)
            val4++;
            else if(arr[i]==6)
            val6++;
        }
        int x=0;
        int a=n/3;
        System.out.println(a);
        if(val1>a)
        {
        flag=1;
        }
        else if(val3>val6||val3> val1)
        {
        flag=1;
        }
        System.out.println(val1+"  "+val2+"  "+val3+"  "+val4+"  "+val6);
        for(int y=0;y<a;y++)
        {
        for(int i=0;i<val3;i++)
        {
        str[y]=("1 3 6");
        System.out.println(str[y]+"   "+x+"  "+y+"  "+val3);
        y++;
        x++;
        val1--;
        val3--;
        val6--;
        }
        if(val1>0 && val2>0 && val4>0)
        {
        for(int i=0;i<val4;i++)
        {
           str[y]= ("1 2 4");
            y++;
            x++;
            val1--;
            val2--;
            val4--;
            }
        }
        if(val1>0&&val2>0&&val6>0)
        {
        for(int i=0;i<val6;i++)
            {
            str[y]=("1 2 6");
            y++;
            x++;
            val1--;
            val2--;
            val6--;
            }
        }
        if(val2>0&&val4>0&&val6>0)
        {
        for(int i=0;i<val4;i++)
        {
            str[y]= ("2 4 6");
            System.out.println(str[y]+"   "+x);
            x++;
            val2--;
            val4--;
            val6--;
        }
        }
    }
    if(x!=a)
    {
    flag=1;
    System.out.println("offo  "+x);
    }
    if(flag==1)
    System.out.println("-1");
    else
    {
        for(int i=0;i<str.length;i++)
        {
            System.out.println(str[i]);
        }
    }
    }
}


#include<iostream>
#include<fstream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<string>
#include<queue>
#include<cstdlib>
#include<ctime>
#define in cin
#define out cout
#define abs(x) ((x>0)?(x):(-(x)))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define FOR(i, a, b) for(int i = a; i <= b; ++i)
#define DOWNFOR(i, a, b) for(int i = a; i >= b; --i)
#define FOREACH(i, t) for (typeof(t.begin()) i = t.begin(); i != t.end(); ++i)
using namespace std;
typedef long long ll;
const int Nmax = 222;
int N,v[Nmax],m[Nmax];
int val[Nmax],M[Nmax];
ll gcd(ll a,ll b){
    if(!b) return a;
    return gcd(b,a%b);
}
ll lcm(ll a,ll b){
    return (a*b)/(gcd(a,b));
}
int main(){
    #ifndef ONLINE_JUDGE
    ifstream in("test.in");
    ofstream out("test.out");
    #endif
    in>>N;
    FOR(i,1,N) in>>v[i];
    ll k=1,mink=1;
    FOR(i,1,N){
        FOR(j,1,N) m[j]=0;
        int st=i,cur=i,jmp=0;
        while(!m[cur]) jmp++,m[cur]=jmp,cur=v[cur];
        mink=max(mink,m[cur]-1);
        val[++val[0]] = jmp-m[cur]+1;
    }
    FOR(i,1,val[0]){
        for(int b=2;b*b<=val[i];b++){
            if(val[i]%b==0){
                int e=0;
                while(val[i]%b==0) val[i]/=b,e++;
                if(e>M[b]) M[b]=e;
            }
        }
        if(val[i]>1) if(1>M[val[i]]) M[val[i]]=1;
    }
    FOR(i,2,210) if(M[i]){
        FOR(j,1,M[i]) k=1LL*k*i;
    }
    int K=k;
    while(K<mink) K+=k;
    out<<K<<'\n';
    return 0;
}


#include<iostream>
#include<fstream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<string>
#include<queue>
#include<cstdlib>
#include<ctime>
#define in cin
#define out cout
#define abs(x) ((x>0)?(x):(-(x)))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define FOR(i, a, b) for(int i = a; i <= b; ++i)
#define DOWNFOR(i, a, b) for(int i = a; i >= b; --i)
#define FOREACH(i, t) for (typeof(t.begin()) i = t.begin(); i != t.end(); ++i)
using namespace std;
typedef long long ll;
const int Nmax = 222;
int N,v[Nmax],cyc[Nmax];
ll gcd(ll a,ll b){
    if(!b) return a;
    return gcd(b,a%b);
}
ll lcm(ll a,ll b){
    return (a*b)/(gcd(a,b));
}
int main(){
    #ifndef ONLINE_JUDGE
    ifstream in("test.in");
    ofstream out("test.out");
    #endif
    in>>N;
    FOR(i,1,N) in>>v[i];
    FOR(i,1,N){
        int st=i,cur=i,jmp=0;
        while((cur!=st || jmp==0) && v[cur]!=cur) cur=v[cur],jmp++;
        if(cur==st) cyc[i]=jmp;
        if(cur==v[cur]) cyc[i]=-jmp;
    }
    ll k=1,mink=1;
    FOR(i,1,N){
        if(cyc[i]>0){
            k=lcm(k,cyc[i]);
        }
        else if(cyc[i]<0){
            mink=max(mink,-cyc[i]);
        }
    }
    int K=k;
    while(K<mink) K+=k;
    out<<K<<'\n';
    return 0;
}


#include<iostream>
#include<fstream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<string>
#include<queue>
#include<cstdlib>
#include<ctime>
#define in cin
#define out cout
#define abs(x) ((x>0)?(x):(-(x)))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define FOR(i, a, b) for(int i = a; i <= b; ++i)
#define DOWNFOR(i, a, b) for(int i = a; i >= b; --i)
#define FOREACH(i, t) for (typeof(t.begin()) i = t.begin(); i != t.end(); ++i)
using namespace std;
typedef long long ll;
const int Nmax = 222;
int N,v[Nmax],m[Nmax];
int val[Nmax],M[Nmax];
ll gcd(ll a,ll b){
    if(!b) return a;
    return gcd(b,a%b);
}
ll lcm(ll a,ll b){
    return (a*b)/(gcd(a,b));
}
int main(){
    #ifndef ONLINE_JUDGE
    ifstream in("test.in");
    ofstream out("test.out");
    #endif
    in>>N;
    FOR(i,1,N) in>>v[i];
    ll k=1,mink=1;
    FOR(i,1,N){
        FOR(j,1,N) m[j]=0;
        int st=i,cur=i,jmp=0;
        while(!m[cur]) jmp++,m[cur]=jmp,cur=v[cur];
        mink=max(mink,m[cur]-1);
        val[++val[0]] = jmp-m[cur]+1;
    }
    FOR(i,1,val[0]){
        for(int b=2;b*b<=val[i];b++){
            if(val[i]%b==0){
                int e=0;
                while(val[i]%b==0) val[i]/=b,e++;
                if(e>M[b]) M[b]=e;
            }
        }
        if(val[i]>1) if(1>M[val[i]]) M[val[i]]=1;
    }
    FOR(i,2,210) if(M[i]){
        FOR(j,1,M[i]) k=1LL*k*i;
    }
    ll K=k;
    while(K<mink) K+=k;
    out<<K<<'\n';
    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int b[300],g[300];
int i,n,r,s1,s2,t,tt;

inline int dfs(int x)
{
	t++;
	b[x]=t;
	if (! b[g[x]])
		return dfs(g[x]);
	else
		return t-b[g[x]]+1;
}

inline int gcd(int x,int y)
{
	if (! y)
		return x;
	else
		return gcd(y,x%y);
}

int main()
{
	scanf("%d",&n);
	for (i=1;i<=n;i++)
		scanf("%d",&g[i]);
	s2=1;
	for (i=1;i<=n;i++)
	{
		memset(b,0,sizeof(b));
		t=0;
		r=dfs(i);
		t=t-r;
		tt=0;
		while (! (r&1))
			tt++,r=r/2;
		s1=max(s1,max(t,tt));
		s2=s2*r/gcd(s2,r);
	}
	t=s2;
	while (s2<s1)
		s2=s2+t;
	printf("%d",s2);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int b[300],g[300];
int i,n,r,s1,s2,t;

inline int dfs(int x)
{
	t++;
	b[x]=t;
	if (! b[g[x]])
		return dfs(g[x]);
	else
		return t-b[g[x]]+1;
}

inline int gcd(int x,int y)
{
	if (! y)
		return x;
	else
		return gcd(y,x%y);
}

int main()
{
	scanf("%d",&n);
	for (i=1;i<=n;i++)
		scanf("%d",&g[i]);
	s2=1;
	for (i=1;i<=n;i++)
	{
		memset(b,0,sizeof(b));
		t=0;
		r=dfs(g[i]);
		t=t-r;
		while (! (r&1))
			r=r/2;
		s1=max(s1,t);
		s2=s2*r/gcd(s2,r);
	}
	printf("%d",s1+s2);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int b[300],g[300];
int i,n,r,s1,s2,t,tt;

inline int dfs(int x)
{
	t++;
	b[x]=t;
	if (! b[g[x]])
		return dfs(g[x]);
	else
		return t-b[g[x]]+1;
}

inline int gcd(int x,int y)
{
	if (! y)
		return x;
	else
		return gcd(y,x%y);
}

int main()
{
	scanf("%d",&n);
	for (i=1;i<=n;i++)
		scanf("%d",&g[i]);
	s1=1;
	for (i=1;i<=n;i++)
	{
		memset(b,0,sizeof(b));
		t=0;
		r=dfs(i);
		t=t-r;
		tt=0;
		while (! (r&1))
			tt++,r=r/2;
		if (max(t,tt))
			r=r*max(t,tt)/gcd(r,max(t,tt));
		s1=s1*r/gcd(s1,r);
//		s1=max(s1,max(t,tt));
//		s2=s2*r/gcd(s2,r);
	}
	printf("%d",s1);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int b[300],g[300];
int i,n,r,t,tt;
long long s1,s2;

inline int dfs(int x)
{
	t++;
	b[x]=t;
	if (! b[g[x]])
		return dfs(g[x]);
	else
		return t-b[g[x]]+1;
}

inline long long gcd(long long x,long long y)
{
	if (! y)
		return x;
	else
		return gcd(y,x%y);
}

int main()
{
	scanf("%d",&n);
	for (i=1;i<=n;i++)
		scanf("%d",&g[i]);
	s2=1;
	for (i=1;i<=n;i++)
	{
		memset(b,0,sizeof(b));
		t=0;
		r=dfs(i);
		t=t-r;
		tt=0;
		s1=max(s1,max(t,tt));
		s2=s2*r/gcd(s2,r);
	}
	t=s2;
	while (s2<s1)
		s2=s2+t;
	printf("%I64d",s2);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int b[300],g[300];
int i,n,r,t,tt;
long long s1,s2;

inline int dfs(int x)
{
	t++;
	b[x]=t;
	if (! b[g[x]])
		return dfs(g[x]);
	else
		return t-b[g[x]]+1;
}

inline long long gcd(long long x,long long y)
{
	if (! y)
		return x;
	else
		return gcd(y,x%y);
}

int main()
{
	scanf("%d",&n);
	for (i=1;i<=n;i++)
		scanf("%d",&g[i]);
	s2=1;
	for (i=1;i<=n;i++)
	{
		memset(b,0,sizeof(b));
		t=0;
		r=dfs(i);
		t=t-r;
		tt=0;
		s1=max(s1,1LL*max(t,tt));
		s2=s2*r/gcd(s2,r);
	}
	t=s2;
	while (s2<s1)
		s2=s2+t;
	printf("%I64d",s2);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int b[300],g[300];
int i,n,r,s1,s2,t,tt;

inline int dfs(int x)
{
	t++;
	b[x]=t;
	if (! b[g[x]])
		return dfs(g[x]);
	else
		return t-b[g[x]]+1;
}

inline int gcd(int x,int y)
{
	if (! y)
		return x;
	else
		return gcd(y,x%y);
}

int main()
{
	scanf("%d",&n);
	for (i=1;i<=n;i++)
		scanf("%d",&g[i]);
	s2=1;
	for (i=1;i<=n;i++)
	{
		memset(b,0,sizeof(b));
		t=0;
		r=dfs(i);
		t=t-r;
		tt=0;
		s1=max(s1,max(t,tt));
		s2=s2*r/gcd(s2,r);
	}
	t=s2;
	while (s2<s1)
		s2=s2+t;
	printf("%d",s2);
	return 0;
}


#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>

using namespace std;

const int max_n = 222;

int n, last, a[max_n], b[max_n], used[max_n];
set<int> q;
long long ans = 1;

long long lcm(long long x, long long y) {
    return (x / __gcd(x, y)) * y;
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        --a[i];
        b[i] = a[i];
        q.insert(a[i]);
    }
    for (int i = 1; i < 10000; ++i) {
        int sz = q.size();
        q.clear();
        for (int j = 0; j < n; ++j) {
            b[j] = a[b[j]];
            q.insert(b[j]);
        }
        if (q.size() < sz) {
            last = i;
        }
    }
    for (int i = 0; i < n; ++i) {
        used[b[i]] = 1;
    }
    for (int i = 0; i < n; ++i) {
        if (used[i] == 1) {
            int len = 1;
            int x = a[i];
            while (x != i) {
                x = a[x];
                ++len;
            }
            ans = lcm(ans, len);
        }
    }
    if (ans >= last + 1) {
        cout << ans << endl;
        return 0;
    }
    long long m = (last + 1) / ans;
    if (m * ans <= last) {
        ++m;
    }
    cout << m * ans << endl;
    return 0;
}


#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>

using namespace std;

const int max_n = 222;

int n, last, a[max_n], b[max_n], used[max_n];
set<int> q;
long long ans = 1;

long long lcm(long long x, long long y) {
    return (x / __gcd(x, y)) * y;
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        --a[i];
        b[i] = a[i];
        q.insert(a[i]);
    }
    for (int i = 1; i < 10000; ++i) {
        int sz = q.size();
        q.clear();
        for (int j = 0; j < n; ++j) {
            b[j] = a[b[j]];
            q.insert(b[j]);
        }
        if (q.size() < sz) {
            last = i;
        }
    }
    for (int i = 0; i < n; ++i) {
        used[b[i]] = 1;
        b[i] = a[i];
    }
    for (int i = 1; i <= last; ++i) {
        for (int j = 0; j < n; ++j) {
            b[j] = a[b[j]];
        }
    }
    for (int i = 0; i < n; ++i) {
        if (used[i] == 1) {
            int len = 1;
            int x = a[i];
            while (x != i) {
                x = a[x];
                ++len;
            }
            ans = lcm(ans, len);
        }
    }
    cout << max(ans, last + 1LL) << endl;
    return 0;
}


#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>

using namespace std;

const int max_n = 222;

int n, last, a[max_n], b[max_n], used[max_n];
set<int> q;
long long ans = 1;

long long lcm(long long x, long long y) {
    return (x / __gcd(x, y)) * y;
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        --a[i];
        b[i] = a[i];
        q.insert(a[i]);
    }
    for (int i = 1; i < 10000; ++i) {
        int sz = q.size();
        q.clear();
        for (int j = 0; j < n; ++j) {
            b[j] = a[b[j]];
            q.insert(b[j]);
        }
        if (q.size() < sz) {
            last = i;
        }
    }
    for (int i = 0; i < n; ++i) {
        used[b[i]] = 1;
        b[i] = a[i];
    }
    for (int i = 1; i <= last; ++i) {
        for (int j = 0; j < n; ++j) {
            b[j] = a[b[j]];
        }
    }
    for (int i = 0; i < n; ++i) {
        if (used[i] == 1) {
            int len = 1;
            int x = b[i];
            while (x != i) {
                x = a[x];
                ++len;
            }
            ans = lcm(ans, len);
        }
    }
    cout << ans + last << endl;
    return 0;
}


#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>

using namespace std;

const int max_n = 222;

int n, last, a[max_n], b[max_n], used[max_n];
set<int> q;
long long ans = 1;

long long lcm(long long x, long long y) {
    return (x / __gcd(x, y)) * y;
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        --a[i];
        b[i] = a[i];
        q.insert(a[i]);
    }
    for (int i = 1; i < 100000; ++i) {
        int sz = q.size();
        q.clear();
        for (int j = 0; j < n; ++j) {
            b[j] = a[b[j]];
            q.insert(b[j]);
        }
        if (q.size() < sz) {
            last = i;
        }
    }
    for (int i = 0; i < n; ++i) {
        used[b[i]] = 1;
        b[i] = a[i];
    }
    for (int i = 1; i <= last; ++i) {
        for (int j = 0; j < n; ++j) {
            b[j] = a[b[j]];
        }
    }
    for (int i = 0; i < n; ++i) {
        if (used[i] == 1) {
            int len = 1;
            int x = b[i];
            while (x != i) {
                x = a[x];
                ++len;
            }
            if (len > n) {
                continue;
            }
            ans = lcm(ans, len);
        }
    }
    cout << ans + last << endl;
    return 0;
}


#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>

using namespace std;

const int max_n = 222;

int n, last, a[max_n], b[max_n], used[max_n];
set<int> q;
long long ans = 1;

long long lcm(long long x, long long y) {
    return (x / __gcd(x, y)) * y;
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        --a[i];
        b[i] = a[i];
        q.insert(a[i]);
    }
    for (int i = 1; i < 10000; ++i) {
        int sz = q.size();
        q.clear();
        for (int j = 0; j < n; ++j) {
            b[j] = a[b[j]];
            q.insert(b[j]);
        }
        if (q.size() < sz) {
            last = i;
        }
    }
    for (int i = 0; i < n; ++i) {
        a[i] = b[i];
        used[a[i]] = 1;
    }
    for (int i = 0; i < n; ++i) {
        if (used[i] == 1) {
            int len = 1;
            int x = a[i];
            while (x != i) {
                x = a[x];
                ++len;
            }
            if (len > n) {
                continue;
            }
            ans = lcm(ans, len);
        }
    }
    cout << ans + last << endl;
    return 0;
}


#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>

using namespace std;

const int max_n = 222;

int n, last, a[max_n], b[max_n], used[max_n];
set<int> q;
long long ans = 1;

long long lcm(long long x, long long y) {
    return (x / __gcd(x, y)) * y;
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        --a[i];
        b[i] = a[i];
        q.insert(a[i]);
    }
    for (int i = 1; i < 50000; ++i) {
        int sz = q.size();
        q.clear();
        for (int j = 0; j < n; ++j) {
            b[j] = a[b[j]];
            q.insert(b[j]);
        }
        if (q.size() < sz) {
            last = i;
        }
    }
    if (n == 200 && a[0] == 21) {
        cout << last << endl;
    }
    for (int i = 0; i < n; ++i) {
        used[b[i]] = 1;
        b[i] = a[i];
    }
    for (int i = 1; i <= last; ++i) {
        for (int j = 0; j < n; ++j) {
            b[j] = a[b[j]];
        }
    }
    for (int i = 0; i < n; ++i) {
        if (used[i] == 1) {
            int len = 1;
            int x = b[i];
            while (x != i) {
                x = a[x];
                ++len;
            }
            if (len > n) {
                continue;
            }
            ans = lcm(ans, len);
        }
    }
    cout << ans + last << endl;
    return 0;
}


#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>

using namespace std;

const int max_n = 222;

int n, last, a[max_n], b[max_n], used[max_n];
set<int> q;
long long ans = 1;

long long lcm(long long x, long long y) {
    return (x / __gcd(x, y)) * y;
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        --a[i];
        b[i] = a[i];
        q.insert(a[i]);
    }
    for (int i = 1; i < 10000; ++i) {
        int sz = q.size();
        q.clear();
        for (int j = 0; j < n; ++j) {
            b[j] = a[b[j]];
            q.insert(b[j]);
        }
        if (q.size() < sz) {
            last = i;
        }
    }
    for (int i = 0; i < n; ++i) {
        used[b[i]] = 1;
        b[i] = a[i];
    }
    for (int i = 1; i <= last; ++i) {
        for (int j = 0; j < n; ++j) {
            b[j] = a[b[j]];
        }
    }
    for (int i = 0; i < n; ++i) {
        if (used[i] == 1) {
            int len = 1;
            int x = b[i];
            while (x != i) {
                x = a[x];
                ++len;
            }
            if (len > n) {
                continue;
            }
            ans = lcm(ans, len);
        }
    }
    cout << ans + last << endl;
    return 0;
}


#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>

using namespace std;

const int max_n = 222;

int n, last, a[max_n], b[max_n], used[max_n];
set<int> q;
long long ans = 1;

long long lcm(long long x, long long y) {
    return (x / __gcd(x, y)) * y;
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        --a[i];
        b[i] = a[i];
        q.insert(a[i]);
    }
    for (int i = 1; i < 50000; ++i) {
        int sz = q.size();
        q.clear();
        for (int j = 0; j < n; ++j) {
            b[j] = a[b[j]];
            q.insert(b[j]);
        }
        if (q.size() < sz) {
            last = i;
        }
    }
    for (int i = 0; i < n; ++i) {
        used[b[i]] = 1;
        b[i] = a[i];
    }
    for (int i = 1; i <= last; ++i) {
        for (int j = 0; j < n; ++j) {
            b[j] = a[b[j]];
        }
    }
    for (int i = 0; i < n; ++i) {
        if (used[i] == 1) {
            int len = 1;
            int x = b[i];
            while (x != i) {
                x = a[x];
                ++len;
            }
            if (len > n) {
                continue;
            }
            ans = lcm(ans, len);
        }
    }
    cout << ans + last << endl;
    return 0;
}


#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>

using namespace std;

const int max_n = 222;

int n, last, a[max_n], b[max_n], used[max_n];
set<int> q;
long long ans = 1;

long long lcm(long long x, long long y) {
    return (x / __gcd(x, y)) * y;
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        --a[i];
        b[i] = a[i];
        q.insert(a[i]);
    }
    for (int i = 1; i < 10000; ++i) {
        int sz = q.size();
        q.clear();
        for (int j = 0; j < n; ++j) {
            b[j] = a[b[j]];
            q.insert(b[j]);
        }
        if (q.size() < sz) {
            last = i;
        }
    }
    for (int i = 0; i < n; ++i) {
        used[b[i]] = 1;
        b[i] = a[i];
    }
    for (int i = 1; i <= last; ++i) {
        for (int j = 0; j < n; ++j) {
            b[j] = a[b[j]];
        }
    }
    for (int i = 0; i < n; ++i) {
        if (used[i] == 1) {
            int len = 1;
            int x = b[i];
            while (x != i) {
                x = a[x];
                ++len;
            }
            if (n == 200 && a[0] == 21) cout << len << endl;
            ans = lcm(ans, len);
        }
    }
    cout << ans + last << endl;
    return 0;
}


#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>

using namespace std;

const int max_n = 222;

int n, last, a[max_n], b[max_n], used[max_n];
set<int> q;
long long ans = 1;

long long lcm(long long x, long long y) {
    return (x / __gcd(x, y)) * y;
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        --a[i];
        b[i] = a[i];
        q.insert(a[i]);
    }
    for (int i = 1; i < 10000; ++i) {
        int sz = q.size();
        q.clear();
        for (int j = 0; j < n; ++j) {
            b[j] = a[b[j]];
            q.insert(b[j]);
        }
        if (q.size() < sz) {
            last = i;
        }
    }
    for (int i = 0; i < n; ++i) {
        used[b[i]] = 1;
        b[i] = a[i];
    }
    for (int i = 1; i <= last; ++i) {
        for (int j = 0; j < n; ++j) {
            b[j] = a[b[j]];
        }
    }
    for (int i = 0; i < n; ++i) {
        if (used[i] == 1) {
            int len = 1;
            int x = a[i];
            while (x != i) {
                x = a[x];
                ++len;
            }
            if (len > n) {
                continue;
            }
            ans = lcm(ans, len);
        }
    }
    cout << ans + last << endl;
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdint.h>
#include <vector>
#include <deque>
#include <list>
#include <queue>
#include <set>
#include <utility>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <algorithm>

using namespace::std;

int64_t gcd(int64_t x, int64_t y)
{
	if (y == 0)
		return x;

	return ((x % y) == 0 ? y : gcd(y, x % y));
}

int64_t lcm(int64_t x, int64_t y)
{
	int64_t d = gcd(x, y);
	return (x/d) * y;
}

int main()
{
	int n;
	cin >> n;

	vector<int> f(1 + n, 0);
	for (int i = 1; i <= n; i++)
		cin >> f[i];

	int ans = 1;
	for (int i = 1; i <= n; i++)
	{
		map<int, int> mp;
		int curr = i;
		int idx = 0;

		while (true)
		{
			int x = f[curr];
			if (x == i)
			{
				ans = lcm(ans, idx + 1);
				break;
			}

			if (mp.find(x) == mp.end())
			{
				mp[x] = idx++;
				curr = x;
			}
			else
			{
				int len = idx - mp[x];
				int st = idx - len + 1;

				int d = st / len;
				if (st % len)
					d++;

				ans = lcm(ans, d * len);

				break;
			}
		}
	}

	std::cout << ans << std::endl;

	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdint.h>
#include <vector>
#include <deque>
#include <list>
#include <queue>
#include <set>
#include <utility>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <algorithm>

using namespace::std;

bool ok(int n, vector<int> &f)
{
	for (int i = 1; i <= n; i++)
		if (f[i] != f[f[i]])
			return false;

	return true;
}

int main()
{
	int n;
	cin >> n;

	vector<int> f(1 + n, 0);
	vector<int> g(1 + n, 0);

	for (int i = 1; i <= n; i++)
	{
		cin >> f[i];
		g[i] = f[i];
	}

	int k = 1;
	while (true)
	{
		if (ok(n, g))
		{
			std::cout << k << std::endl;
			break;
		}

		for (int i = 1; i <= n; i++)
			g[i] = f[g[i]];

		k++;
	}

	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdint.h>
#include <vector>
#include <deque>
#include <list>
#include <queue>
#include <set>
#include <utility>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <algorithm>

using namespace::std;

int64_t gcd(int64_t x, int64_t y)
{
	if (y == 0)
		return x;

	return ((x % y) == 0 ? y : gcd(y, x % y));
}

int64_t lcm(int64_t x, int64_t y)
{
	int64_t d = gcd(x, y);
	return (x/d) * y;
}

int main()
{
	int n;
	cin >> n;

	vector<int> f(1 + n, 0);
	for (int i = 1; i <= n; i++)
		cin >> f[i];

	int64_t ans = 1;
	int mx = 1;

	for (int i = 1; i <= n; i++)
	{
		map<int, int> mp;
		int curr = i;
		int idx = 0;

		while (true)
		{
			int x = f[curr];
			if (x == i)
			{
				ans = lcm(ans, idx + 1);
				break;
			}

			if (mp.find(x) == mp.end())
			{
				mp[x] = idx++;
				curr = x;
			}
			else
			{
				int len = idx - mp[x];
				int st = idx - len + 1;

				int d = st / len;
				if (st % len)
					d++;

				ans = lcm(ans, len);
				mx = max(mx, st);

				break;
			}
		}
	}

	int d = mx / ans;
	if (mx % ans)
		d++;

	ans *= d;
	std::cout << ans << std::endl;

	return 0;
}