//get the size of the problem
int n;
int num = 1;
cin>>n;
//calculate num = size!
for (int i = 1; i <= n; i++)
num *= i;
//get the array
int* a = new int[n];
for (int i = 0; i < n; i++)
a[i] = i;
//generate the permutation
int j,k, temp;
int r, s;
for (int i = 0; i < num; i++) {
j = n - 1;
k = n;
while (a[j] > a[j + 1]) j--;
while (a[j] > a[k]) k--;
temp = a[j];
a[j] = a[k];
a[k] = temp;
//partition the array after j
r = n;
s = j + 1;
while (r > s) {
temp = a[r];
a[r] = a[s];
a[s] = temp;
r--;
s++;
}
const
MAX_N = 1000;
var
used: array [1..MAX_N] of boolean;
P: array [1..MAX_N] of integer;
n:integer;
procedure search(k:integer);
var
i:integer;
begin
if k=n+1
then begin
for i:=1 to n do write(P[i]);
writeln;
end
else for i:=1 to n do
if not used[i] then
begin
used[i] := true;
P[k] := i;
search(k+1);
used[i] := false;
end;
end;
begin
fillchar(used, sizeof(used), false);
write('input n:');readln(n);
search(1);
readln;
end.