/*  -*- coding: utf-8 -*-
 *
 * 2847.cc: Ninja Map
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100;
const int MAX_NN = MAX_N * MAX_N;

/* typedef */

typedef vector<int> vi;

/* global variables */

vi nbrs[MAX_NN];
int xs[MAX_NN], ys[MAX_NN], flds[MAX_N][MAX_N];

/* subroutines */

void printflds(int n) {
  for (int y = 0; y < n; y++) {
    for (int x = 0; x < n; x++) {
      if (x) putchar(' ');
      printf("%d", flds[y][x] + 1);
    }
    putchar('\n');
  }
}

/* main */

int main() {
  int n;
  scanf("%d", &n);

  int nn = n * n;
  int m = 2 * nn - 2 * n;

  for (int i = 0; i < m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--, b--;
    nbrs[a].push_back(b);
    nbrs[b].push_back(a);
  }

  memset(xs, -1, sizeof(xs));
  memset(ys, -1, sizeof(ys));
  memset(flds, -1, sizeof(flds));

  int st = -1;
  for (int i = 0; st < 0 && i < nn; i++)
    if (nbrs[i].size() == 2) st = i;
  xs[st] = ys[st] = 0;
  flds[0][0] = st;
  //printf("st=%d\n", st);

  for (int i = nbrs[st][0], x = 1;; x++) {
    xs[i] = x, ys[i] = 0;
    flds[0][x] = i;
    vi &nbri = nbrs[i];
    if (nbri.size() < 3) break;

    int j = -1;
    for (vi::iterator vit = nbri.begin(); vit != nbri.end(); vit++)
      if (xs[*vit] < 0 && nbrs[*vit].size() < 4) {
	j = *vit;
	break;
      }
    i = j;
  }

  for (int y = 1; y < n; y++)
    for (int x = 0; x < n; x++) {
      int u = flds[y - 1][x];
      vi &nbru = nbrs[u];
      int v = -1;
      for (vi::iterator vit = nbru.begin(); vit != nbru.end(); vit++)
	if (xs[*vit] < 0) {
	  v = *vit;
	  break;
	}

      xs[v] = x, ys[v] = y;
      flds[y][x] = v;
    }
  printflds(n);
  
  return 0;
}

