import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;

void main() {
    auto N = readln.chomp.to!int;
    auto S = readln.chomp;
    auto T = S.map!(s => to!int(s - '1')).array;
    auto P = 9.iota.array;
    auto Q = [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]];

    auto mp = new int[][](9, 9);
    foreach (i; 0..N-1) mp[T[i]][T[i+1]] += 1;

    int ansv = 1 << 29;
    int[] ans;

    int[] trans(int[] a) {
        auto b = new int[](9);
        foreach (i; 0..9) b[a[i]] = i;
        return b;
    }

    do {
        int tmp = 0;
        foreach (i; 0..9) {
            foreach (j; 0..9) {
                int dist = abs(Q[P[i]][0] - Q[P[j]][0]) + abs(Q[P[i]][1] - Q[P[j]][1]);
                tmp += dist * mp[i][j];
            }
        }
        if (tmp < ansv) {
            ansv = tmp;
            ans = trans(P);
        } else if (tmp == ansv) {
            auto b = trans(P);
            if (b < ans) ans = b.dup;
        }
    } while (nextPermutation(P));

    foreach (i; 0..3) {
        foreach (j; 0..3) {
            write(ans[i*3+j]+1);
        }
        writeln;
    }
}

