Go: package main

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

func configure(scanner *bufio.Scanner) {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
}
func getNextString(scanner *bufio.Scanner) string {
	scanned := scanner.Scan()
	if !scanned {
		panic("scan failed")
	}
	return scanner.Text()
}
func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}
func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}
func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	extra := 0
	if os.Getenv("I") == "IronMan" {
		fp, _ = os.Open(os.Getenv("END_GAME"))
		extra = 100
	}
	scanner := bufio.NewScanner(fp)
	configure(scanner)
	writer := bufio.NewWriter(wfp)
	defer func() {
		r := recover()
		if r != nil {
			fmt.Fprintln(writer, r)
		}
		writer.Flush()
	}()
	solve(scanner, writer)
	for i := 0; i < extra; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	n := getNextInt(scanner)
	s := getNextString(scanner)
	cc := makeGrid(9, 9)
	for i := 0; i < n-1; i++ {
		cc[s[i]-'0'-1][s[i+1]-'0'-1]++
	}
	p := make(permutation, 9)
	ans := make([]int, 9)
	p.dfs(0, 9, 0, ans, cc)
	for i := 0; i < 9; i++ {
		fmt.Fprint(writer, ans[i]+1)
		if i%3 == 2 {
			fmt.Fprintln(writer, "")
		}
	}
}
func makeGrid(h, w int) [][]int {
	index := make([][]int, h, h)
	data := make([]int, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

var min int = math.MaxInt32

type permutation []int

func (p permutation) dfs(i, n int, used uint32, ans []int, cc [][]int) {
	if i == n {
		sum := getsum(p, cc)
		if min > sum {
			for i := 0; i < 9; i++ {
				ans[i] = p[i]
			}
			min = sum
		}
		return
	}
	for b := 0; b < 9; b++ {
		if used&(1<<uint(b)) != 0 {
			continue
		}
		p[i] = b
		p.dfs(i+1, n, used|(1<<uint(b)), ans, cc)
	}
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}
func getsum(p permutation, cc [][]int) int {
	sum := 0
	for i := 0; i < 9; i++ {
		for j := 0; j < 9; j++ {
			if i == j {
				continue
			}
			sum += (abs(i/3-j/3) + abs(i%3-j%3)) * cc[p[i]][p[j]]
		}
	}
	return sum
}

C++: #define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=3053"

#include<bits/stdc++.h>
using namespace std;

#define call_from_test
#ifndef call_from_test
#include<bits/stdc++.h>
using namespace std;
#endif
//BEGIN CUT HERE
struct FastIO{
  FastIO(){
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
}fastio_beet;
//END CUT HERE
#ifndef call_from_test
signed main(){
  return 0;
}
#endif

#ifndef call_from_test
#include<bits/stdc++.h>
using namespace std;
#endif
//BEGIN CUT HERE
template<typename F>
void all_permutations(int n,F f){
  vector<int> vs(n);
  iota(vs.begin(),vs.end(),0);
  do f(vs); while(next_permutation(vs.begin(),vs.end()));
}
//END CUT HERE
#ifndef call_from_test
//INSERT ABOVE HERE
signed ABC054_C(){
  int n,m;
  cin>>n>>m;
  int es[10][10]={};
  for(int i=0;i<m;i++){
    int a,b;
    cin>>a>>b;
    a--;b--;
    es[a][b]=es[b][a]=1;
  }
  int ans=0;
  auto f=
    [&](const auto &vs){
      if(!es[0][vs[0]+1]) return;
      for(int i=0;i+1<n-1;i++)
        if(!es[vs[i]+1][vs[i+1]+1]) return;
      ans++;
    };
  all_permutations(n-1,f);
  cout<<ans<<endl;
  return 0;
}
/*
  verified on 2019/12/09
  https://atcoder.jp/contests/abc054/tasks/abc054_c
*/

signed main(){
  ABC054_C();
  return 0;
}
#endif

#undef call_from_test

signed main(){
  int n;
  string s;
  cin>>n>>s;

  int c[10][10]={};
  for(int i=0;i+1<n;i++){
    int a=s[i]-'0',b=s[i+1]-'0';
    c[a][b]++;
  }

  int ans=n*100;
  vector<int> us;
  auto f=
    [&](auto vs){
      int x[10],y[10];
      for(int i=0;i<9;i++){
        x[vs[i]+1]=i%3;
        y[vs[i]+1]=i/3;
      }

      int res=0;
      for(int i=1;i<=9;i++)
        for(int j=1;j<=9;j++)
          res+=c[i][j]*(abs(x[i]-x[j])+abs(y[i]-y[j]));

      if(res<ans) ans=res,us=vs;
    };
  all_permutations(9,f);

  for(int i=0;i<9;i++){
    cout<<us[i]+1;
    if(i%3==2) cout<<endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ull = unsigned long long;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }

const int d4[4][2] = {
        {1, 0}, {0, 1}, {-1, 0}, {0, -1}
};

int main() {
    int n;
    string s;
    cin >> n >> s;
    int mp[9][9] = {};
    for (int i = 0; i < n - 1; i++) {
        mp[s[i] - '1'][s[i + 1] - '1']++;
    }

    V<int> idx(9);
    iota(idx.begin(), idx.end(), 0);
    int mv = TEN(9);
    V<int> ans;
    do {
        int di[9][9] = {};
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                di[i][j] = TEN(9);
            }
            di[i][i] = 0;
        }
        for (int i = 0; i < 9; i++) {
            int y = i / 3, x = i % 3;
            for (int d = 0; d < 4; d++) {
                int ny = y + d4[d][0], nx = x + d4[d][1];
                if (ny < 0 || 3 <= ny || nx < 0 || 3 <= nx) continue;
                int j = ny * 3 + nx;
                di[idx[i]][idx[j]] = 1;
            }
        }
        for (int k = 0; k < 9; k++) {
            for (int i = 0; i < 9; i++) {
                for (int j = 0; j < 9; j++) {
                    di[i][j] = min(di[i][j], di[i][k] + di[k][j]);
                }
            }
        }
        int sm = 0;
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                sm += di[i][j] * mp[i][j];
            }
        }
        if (sm < mv) {
            mv = sm;
            ans = idx;
        }
    } while (next_permutation(idx.begin(), idx.end()));

    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            cout << ans[i * 3 + j] + 1;
        }
        cout << endl;
    }
    return 0;
}