#include <iostream>
#include <queue>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> adj[100001];
int check[100001];
queue<int> q;
void dfs(int u)
{
if (check[u])
return;
check[u] = 1;
cout << u << " ";
for (int i = 0; i < adj[u].size(); i++)
{
int v = adj[u][i];
dfs(v);
}
}
void bfs(int u)
{
q.push(u);
check[u] = 1;
while (!q.empty())
{
int v = q.front();
q.pop();
cout << v << " ";
for (int i = 0; i < adj[v].size(); i++)
{
int w = adj[v][i];
if (!check[w])
{
q.push(w);
check[w] = 1;
}
}
}
}
int main()
{
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++)
{
int u, v;
cin >> u >> v;
adj[u].push_back(v); // 邻接表
}
for (int i = 1; i <= n; i++)
{
sort(adj[i].begin(), adj[i].end());
}
dfs(1);
cout << endl;
memset(check, 0, sizeof(check));
bfs(1);
return 0;
}
// 5 6 点 边
// 1 2
// 2 3
// 2 5
// 3 5
// 5 4
// 5 1