Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	io := NewIo()
	defer io.Flush()
	N := io.NextInt()
	nodes := make([]Node, N*N+1)
	for i := 0; i < 2*N*N-2*N; i++ {
		a, b := io.NextInt(), io.NextInt()
		nodes[a].adj = append(nodes[a].adj, b)
		nodes[b].adj = append(nodes[b].adj, a)
	}
	ninja(nodes, N, io)
}

type Node struct {
	adj []int
}

func ninja(nodes []Node, N int, io *Io) {
	used := make([]bool, N*N+1)
	m := make([][]int, N)
	fst := 0
	for i := 0; i < N; i++ {
		m[i] = make([]int, N)
		for j := 0; j < N; j++ {
			if len(nodes[i*N+j].adj) == 2 {
				fst = i*N + j
			}
		}
	}
	m[0][0] = fst
	used[fst] = true
	for j := 1; j < N; j++ {
		u := 0
		deg := 3
		if j == N-1 {
			deg = 2
		}
		for _, v := range nodes[m[0][j-1]].adj {
			if !used[v] && len(nodes[v].adj) == deg {
				u = v
			}
		}
		m[0][j] = u
		used[u] = true
	}
	for i := 1; i < N; i++ {
		for j := 0; j < N; j++ {
			u := 0
			for _, v := range nodes[m[i-1][j]].adj {
				if !used[v] {
					u = v
				}
			}
			m[i][j] = u
			used[u] = true
		}
	}
	for _, a := range m {
		io.PrintIntSlice(a)
	}
}

// I/O template
type Io struct {
	reader    *bufio.Reader
	writer    *bufio.Writer
	tokens    []string
	nextToken int
}

func NewIo() *Io {
	return &Io{
		reader: bufio.NewReader(os.Stdin),
		writer: bufio.NewWriter(os.Stdout),
	}
}

func (io *Io) Flush() {
	err := io.writer.Flush()
	if err != nil {
		panic(err)
	}
}

func (io *Io) NextLine() string {
	var buff []byte
	for {
		line, isPrefix, err := io.reader.ReadLine()
		if err != nil {
			panic(err)
		}
		buff = append(buff, line...)
		if !isPrefix {
			break
		}
	}
	return string(buff)
}

func (io *Io) Next() string {
	for io.nextToken >= len(io.tokens) {
		line := io.NextLine()
		io.tokens = strings.Fields(line)
		io.nextToken = 0
	}
	s := io.tokens[io.nextToken]
	io.nextToken++
	return s
}

func (io *Io) NextInt() int {
	i, err := strconv.Atoi(io.Next())
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) NextFloat() float64 {
	i, err := strconv.ParseFloat(io.Next(), 64)
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) Println(a ...interface{}) {
	fmt.Fprintln(io.writer, a...)
}

func (io *Io) Printf(format string, a ...interface{}) {
	fmt.Fprintf(io.writer, format, a...)
}

func (io *Io) PrintIntSlice(a []int) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func (io *Io) PrintStringSlice(a []string) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func Log(name string, value interface{}) {
	fmt.Fprintf(os.Stderr, "%s=%+v\n", name, value)
}

func ExampleIo() {
	io := NewIo()
	defer io.Flush()
	a := make([]int, 0, 3)
	a[0], a[1], a[2] = io.NextInt(), io.NextInt(), io.NextInt()
	io.Println(a)
	io.PrintIntSlice(a)
	Log("io", a)
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<ll, ll>;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
template<typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template<typename T> void chmax(T &a, const T &b) { a = max(a, b); }
struct FastIO {FastIO() { cin.tie(0); ios::sync_with_stdio(0); }}fastiofastio;
#ifdef DEBUG_ 
#include "../program_contest_library/memo/dump.hpp"
#else
#define dump(...)
#endif
const ll INF = 1LL<<60;

int main(void) {
    ll n;
    cin >> n;
    vector<ll> a(2*n*n-2*n), b(2*n*n-2*n), deg(n*n);
    vector<vector<ll>> g(n*n);
    REP(i, 2*n*n-2*n) {
        cin >> a[i] >> b[i];
        a[i]--, b[i]--;
        deg[a[i]]++;
        deg[b[i]]++;
        g[a[i]].push_back(b[i]);
        g[b[i]].push_back(a[i]);
    }

    // dump(g);

    vector<ll> v1, v2, v3;
    REP(i, n*n) {
        if(deg[i] == 2) v1.push_back(i);
        else if(deg[i] == 3) v2.push_back(i);
        else v3.push_back(i);
    }

    ll dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
    auto check = [&](vector<vector<ll>> ans) {
        REP(i, n) REP(j, n) {
            REP(k, 4) {
                ll nx = j + dx[k], ny = i + dy[k];
                if(0 > nx || nx >= n || 0 > ny || ny >= n) continue;
                bool flag = false;
                for(auto to: g[ans[i][j]]) if(ans[ny][nx] == to) flag = true;
                if(!flag) {
                    dump(i, j, ny, nx);
                    return false;
                }
            }
        }
        return true;
    };

    vector<bool> used(n*n);
    vector<vector<ll>> ans(n, vector<ll>(n));

    if(n == 2) {
        sort(ALL(v1));
        do {
            ll idx = 0;
            REP(i, n) REP(j, n) ans[i][j] = v1[idx++];
            if(check(ans)) {
                REP(i, n) REP(j, n) cout << ans[i][j]+1 << (j==n-1?'\n':' ');
                cout << flush;
                exit(0);
            }
        } while(next_permutation(ALL(v1)));
        assert(false);
    }

    auto solve = [&] {
        FOR(i, 2, n-1) {
            if(ans[0][i-1] == -1) return;
            for(auto to: g[ans[0][i-1]]) if(deg[to] == 3 && !used[to]) {
                ans[0][i] = to;
                used[to] = true;
            }
        }
        FOR(i, 1, n-1) {
            if(ans[i-1][0] == -1) return;
            for(auto to: g[ans[i-1][0]]) if(deg[to] == 3 && !used[to]) {
                ans[i][0] = to;
                used[to] = true;
            }
        }
        FOR(i, 1, n-1) {
            if(ans[n-1][i-1] == -1) return;
            for(auto to: g[ans[n-1][i-1]]) if(deg[to] == 3 && !used[to]) {
                ans[n-1][i] = to;
                used[to] = true;
            }
        }
        FOR(i, 1, n-1) {
            // if(i == 2) {
            //     dump(g[3968]);
            //     dump(i, ans[i-1][n-1]);
            //     dump(ans[0]);
            //     dump(ans[1]);
            // }
            if(ans[i-1][n-1] == -1) return;
            for(auto to: g[ans[i-1][n-1]]) {
                if(deg[to] == 3 && !used[to]) {
                    ans[i][n-1] = to;
                    used[to] = true;
                }
            }
        }
        // dump("hen");
        // 中央
        FOR(i, 1, n-1) FOR(j, 1, n-1) {
            // ans[i][j] の値は ans[i-1][j] と ans[i][j-1] に隣接するやつで一意に決まる
            set<ll> st;
            if(ans[i-1][j]==-1 || ans[i][j-1]==-1) return;
            for(auto to1: g[ans[i-1][j]]) st.insert(to1);
            for(auto to2: g[ans[i][j-1]]) {
                if(st.find(to2) != st.end() && !used[to2]) {
                    ans[i][j] = to2;
                    used[to2] = true;
                }
            }
        }
        // dump("tyuou");
        // dump(ans);
        if(check(ans)) {
            REP(i, n) REP(j, n) cout << ans[i][j]+1 << (j==n-1?'\n':' ');
            cout << flush;
            exit(0);
        }
    };

    sort(ALL(v1));
    do {
        dump(v1);
        REP(i, n) REP(j, n) ans[i][j] = -1;
        used.assign(n*n, false);
        ans[0][0] = v1[0]; used[v1[0]] = true;
        ans[0][n-1] = v1[1]; used[v1[1]] = true;
        ans[n-1][0] = v1[2]; used[v1[2]] = true;
        ans[n-1][n-1] = v1[3]; used[v1[3]] = true;
        ans[0][1] = g[ans[0][0]][0]; used[ans[0][1]] = true;
        solve();

        REP(i, n) REP(j, n) ans[i][j] = -1;
        used.assign(n*n, false);
        ans[0][0] = v1[0]; used[v1[0]] = true;
        ans[0][n-1] = v1[1]; used[v1[1]] = true;
        ans[n-1][0] = v1[2]; used[v1[2]] = true;
        ans[n-1][n-1] = v1[3]; used[v1[3]] = true;
        ans[0][1] = g[ans[0][0]][1]; used[ans[0][1]] = true;
        solve();
    } while(next_permutation(ALL(v1)));
    
    return 0;
}