import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
class GFG {
static void uniquePermutationsUtil(List<String> permute)
{
List<String> p = new ArrayList<>();
for (String i : permute) {
p.add(i);
}
List<String> newList = new ArrayList<>();
for (String i : p) {
if (!newList.contains(i)) {
newList.add(i);
}
}
List<Integer> permuteList = new ArrayList<>();
for (String i : newList) {
int s = Integer.parseInt(i);
permuteList.add(s);
}
for (int i : permuteList) {
System.out.print(i + " ");
}
}
static void uniquePermutations(int N)
{
String num = Integer.toString(N);
List<Character> lis = new ArrayList<>();
for (char c : num.toCharArray()) {
lis.add(c);
}
Collections.sort(lis);
List<String> permute = new ArrayList<>();
do {
char[] charArray = new char[lis.size()];
for (int i = 0; i < lis.size(); i++) {
charArray[i] = lis.get(i);
}
permute.add(new String(charArray));
} while (nextPermutation(lis));
uniquePermutationsUtil(permute);
}
static boolean nextPermutation(List<Character> array)
{
int i = array.size() - 1;
while (i > 0 && array.get(i - 1) >= array.get(i)) {
i--;
}
if (i <= 0) {
return false;
}
int j = array.size() - 1;
while (array.get(j) <= array.get(i - 1)) {
j--;
}
char temp = array.get(i - 1);
array.set(i - 1, array.get(j));
array.set(j, temp);
j = array.size() - 1;
while (i < j) {
temp = array.get(i);
array.set(i, array.get(j));
array.set(j, temp);
i++;
j--;
}
return true;
}
public static void main(String[] args)
{
int N = 7668;
uniquePermutations(N);
}
}