#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <queue>
#include <map>
#include <vector>
#include <unordered_map>
#include <string>
#include <string.h>
using namespace std;
#define lowbit(x) (x & -x)
#define cf int t;scanf("%d",&t);while(t--);
const double eps = 1e-9, pi = acos(-1);
typedef long long ll;
typedef pair<int, int>PII;
template <typename T>
inline void read(T &f)
{
f = 0; T fu = 1; char c = getchar();
while (c<'0' || c>'9') { if (c == '-') { fu = -1; }c = getchar(); }
while (c >= '0'&&c <= '9') { f = (f << 3) + (f << 1) + (c & 15); c = getchar(); }
f *= fu;
}
template <typename T>
void print(T x)
{
if (x < 0) putchar('-'), x = -x;
if (x < 10) putchar(x + 48);
else print(x / 10), putchar(x % 10 + 48);
}
template <typename T>
void print(T x, char t)
{
print(x); putchar(t);
}
const int N = 2e5+10;
int p[20];
bool st[10];
void dfs(int u)
{
if(u==8)
{
for(int i=0;i<8;i++)
printf("%d ",p[i]);
puts("");
}
for(int i=1;i<=8;i++)
{
if(!st[i])
{
p[u]=i;
st[i]=1;
dfs(u+1);
st[i]=0;
}
}
}
void solved()
{
dfs(0);
//priority_queue<int, vector<int>, greater<int>> heap;
;
}
int main()
{
#ifdef AC
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
//cf;
solved();
/*clock_t program_start_clock=clock();
fprintf(stderr,"\nTotal Time: %lf ms",double(clock()-program_start_clock)/(CLOCKS_PER_SEC/1000));*/
return 0;
}
//std::ios::sync_with_stdio(0);
//cin.tie(0);