#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
using namespace std;
typedef pair<int,int> P;

P ne(int N,int x,int y){
    if(x==1 && y!=N) return {x,y+1};
    if(y==N && x!=N) return {x+1,y};
    if(x==N && y!=1) return {x,y-1};
    if(y==1) return {x-1,y};
}

int main(){
    int N;
    cin >> N;
    vector<vector<int>> ans(N+1,vector<int>(N+1,0));
    vector<vector<int>> edge(N*N+1),deg(5);
    vector<int> cnt(N*N+1,0),use(N*N+1,0);
    set<int> s[N*N+1];
    int a,b;
    for(int i=0;i<2*N*N-2*N;i++){
        cin >> a >> b;
        edge[a].push_back(b); edge[b].push_back(a);
        cnt[a]++; cnt[b]++;
        s[a].insert(b); s[b].insert(a);   
    }
    for(int i=1;i<=N*N;i++) deg[cnt[i]].push_back(i);
    int x = 1,y = 1,num = deg[2][0];
    ans[1][1] = num;
    use[num] = 1;
    while(true){
        for(auto a:edge[num]){
            if(!use[a] && cnt[a]!=4){
                P p = ne(N,x,y);
                x = p.first; y = p.second;
                ans[x][y] = a;
                use[a] = 1;
                num = a;
                break;
            }
        }
        if(x==2 && y==1) break;
    }
    for(int i=2;i<=N-1;i++) for(int j=2;j<=N-1;j++){
        for(auto a:s[ans[i-1][j]]){
            if(!use[a] && cnt[a]==4 && s[ans[i][j-1]].count(a)==1){
                ans[i][j] = a;
                use[a] = 1;
                break;
            }
        }
    }
    for(int i=1;i<=N;i++) for(int j=1;j<=N;j++) cout << ans[i][j] << (j!=N? " ":"\n");
}
