// Arup Guha
// 2/10/2015
// Prints out permutations of 0...N-1 in lexicographical order

import java.util.*;

public class perm {

    final public static int N = 4;

    public static void main(String[] args) {
        int[] perm = new int[N];
        boolean[] used = new boolean[N];
        RecPerm(perm, used, 0);
    }

    public static void RecPerm(int[] perm, boolean[] used, int k) {

        // Perm is filled - print it.
        if (k == perm.length) {
            for (int i=0; i<perm.length; i++)
                System.out.print(perm[i]+" ");
            System.out.println();
        }

        // Recursive case.
        else {

            // Try each unused item in slot k and recursively permute rest.
            for (int i=0; i<perm.length; i++) {
                if (!used[i]) {
                    perm[k] = i;
                    used[i] = true;
                    RecPerm(perm, used, k+1);
                    used[i] = false;
                }
            }
        }
    }

}
