#include <bits/stdc++.h>
using namespace std;
int INF = 1000000;
int main(){
  int N;
  cin >> N;
  int M = 2 * N * N - 2 * N;
  vector<vector<int>> E(N * N);
  for (int i = 0; i < M; i++){
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    E[a].push_back(b);
    E[b].push_back(a);
  }
  int s = -1, t = -1;
  for (int i = 0; i < N * N; i++){
    if (E[i].size() == 2){
      s = i;
      break;
    }
  }
  vector<int> d1(N * N, INF);
  d1[s] = 0;
  queue<int> Q1;
  Q1.push(s);
  while (!Q1.empty()){
    int v = Q1.front();
    Q1.pop();
    for (int w : E[v]){
      if (d1[w] == INF){
        d1[w] = d1[v] + 1;
        Q1.push(w);
        if (E[w].size() == 2 && t == -1){
          t = w;
        }
      }
    }
  }
  vector<int> d2(N * N, INF);
  d2[t] = 0;
  queue<int> Q2;
  Q2.push(t);
  while (!Q2.empty()){
    int v = Q2.front();
    Q2.pop();
    for (int w : E[v]){
      if (d2[w] == INF){
        d2[w] = d2[v] + 1;
        Q2.push(w);
      }
    }
  }
  vector<vector<int>> ans(N, vector<int>(N));
  for (int i = 0; i < N * N; i++){
    ans[(d1[i] + d2[i] - N + 1) / 2][(d1[i] - d2[i] + N - 1) / 2] = i + 1;
  }
  for (int i = 0; i < N; i++){
    for (int j = 0; j < N; j++){
      cout << ans[i][j];
      if (j < N - 1){
        cout << ' ';
      }
    }
    cout << endl;
  }
}
