public static void main(String[] args) throws Exception
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        ArrayList<Integer> begin = new ArrayList<Integer>();
        ArrayList<Integer> three = new ArrayList<Integer>();
        ArrayList<Integer> two = new ArrayList<Integer>();
        ArrayList<Integer> other = new ArrayList<Integer>();

        for (int i = 0; i < 20; i++) {
            int j = Integer.parseInt(reader.readLine());
            begin.add(j);
        }

        for (int i = 0; i < 20; i++) {
            int j = begin.get(i);
            if (j%3 == 0 & j%2 == 0){
                three.add(j);
                two.add(j);
            } else if (j%3 == 0){
                three.add(j);
            } else if (j%2 == 0){
                two.add(j);
            }else {
                other.add(j);
            }
        }

        printList(three);
        printList(two);
        printList(other);

    }