public static void main(String[] args) throws Exception
    {
        ArrayList<Integer> list = new ArrayList<Integer>();
        ArrayList<Integer> even = new ArrayList<Integer>();
        ArrayList<Integer> multipleOf3 = new ArrayList<Integer>();
        ArrayList<Integer> others = new ArrayList<Integer>();
        BufferedReader br = new BufferedReader( new InputStreamReader(System.in) );

        for (int i = 0; i < N; i++)
        {
            list.add( Integer.parseInt(br.readLine()) );
        }

        for (Integer x : list)
        {
            if (x % 3 == 0)
            {
                multipleOf3.add(x);
            }
            else if (x % 2 == 0)
            {
                even.add(x);
            }
            else
            {
                others.add(x);
            }
        }

        for (Integer x : multipleOf3)
        {
            System.out.println(x);
        }

        System.out.println();

        for (Integer x : even)
        {
            System.out.println(x);
        }

        System.out.println();

        for (Integer x : others)
        {
            System.out.println(x);
        }
    }