You are given a string $s$ consisting of exactly $n$ characters, and each character is either '0', '1' or '2'. Such strings are called ternary strings.

Your task is to replace minimum number of characters in this string with other characters to obtain a balanced ternary string (balanced ternary string is a ternary string such that the number of characters '0' in this string is equal to the number of characters '1', and the number of characters '1' (and '0' obviously) is equal to the number of characters '2').

Among all possible balanced ternary strings you have to obtain the lexicographically (alphabetically) smallest.

Note that you can neither remove characters from the string nor add characters to the string. Also note that you can replace the given characters only with characters '0', '1' and '2'.

It is guaranteed that the answer exists.


-----Input-----

The first line of the input contains one integer $n$ ($3 \le n \le 3 \cdot 10^5$, $n$ is divisible by $3$) — the number of characters in $s$.

The second line contains the string $s$ consisting of exactly $n$ characters '0', '1' and '2'.


-----Output-----

Print one string — the lexicographically (alphabetically) smallest balanced ternary string which can be obtained from the given one with minimum number of replacements.

Because $n$ is divisible by $3$ it is obvious that the answer exists. And it is obvious that there is only one possible answer.


-----Examples-----
Input
3
121

Output
021

Input
6
000000

Output
001122

Input
6
211200

Output
211200

Input
6
120110

Output
120120

Source: codeforces | Difficulty: MEDIUM
Tags: String algorithms

Greedy algorithms


Solutions:
n = int(input())
s = list(input())
cnt_0 = s.count('0')
cnt_1 = s.count('1')
cnt_2 = s.count('2')
k = n // 3
d = {'0': cnt_0, '1': cnt_1, '2': cnt_2}
for i in range(n):
	if d[s[i]] > k:
		if s[i] == '1' and cnt_1 > k:
			if cnt_0 < k:
				s[i] = '0'
				cnt_0 += 1
				cnt_1 -= 1
		elif s[i] == '2' and cnt_2 > k:
			if cnt_0 < k:
				s[i] = '0'
				cnt_0 += 1
				cnt_2 -= 1
			elif cnt_1 < k:
				s[i] = '1'
				cnt_1 += 1
				cnt_2 -= 1
for j in range(n - 1, -1, -1):
	if d[s[j]] > k:
		if s[j] == '0' and cnt_0 > k:
			if cnt_2 < k:
				s[j] = '2'
				cnt_2 += 1
				cnt_0 -= 1
			elif cnt_1 < k:
				s[j] = '1'
				cnt_1 += 1
				cnt_0 -= 1
		elif s[j] == '1' and cnt_1 > k:
			if cnt_2 < k:
				s[j] = '2'
				cnt_2 += 1
				cnt_1 -= 1
print(*s, sep='')


size = int(input())
ss = input()
s = list()
for i in range(size):
	s.append(ss[i])
ones = twos = zeros = 0
zeroindx = list()
oneindex = list()
twoindex = list()
for i in range(size):
	if s[i] == '0':
		zeros = zeros + 1
		zeroindx.append(i)
	elif s[i] == '1':
		ones = ones + 1
		oneindex.append(i)
	else:
		twos = twos + 1
		twoindex.append(i)
div = int(size / 3)
a = len(zeroindx)
b = len(oneindex)
c = 0
if twos < div:
	while ones > div and twos < div:
		s[oneindex[b - 1]] = '2'
		b = b - 1
		twos = twos + 1
		ones = ones - 1
	while zeros > div and twos < div:
		s[zeroindx[a - 1]] = '2'
		a = a - 1
		twos = twos + 1
		zeros = zeros - 1
if twos > div:
	while zeros < div and twos > div:
		s[twoindex[c]] = '0'
		c = c + 1
		twos = twos - 1
		zeros = zeros + 1
	while ones < div and twos > div:
		s[twoindex[c]] = '1'
		c = c + 1
		twos = twos - 1
		ones = ones + 1
io = 0
while zeros < div:
	s[oneindex[io]] = '0'
	io = io + 1
	zeros = zeros + 1
while ones < div:
	s[zeroindx[a - 1]] = '1'
	a = a - 1
	ones = ones + 1
for i in s:
	print(i, end='')


n = int(input())
s = list(input().rstrip())
(z, o, t) = (s.count('0') - n // 3, s.count('1') - n // 3, s.count('2') - n // 3)
(zcnt, ocnt, tcnt) = (s.count('0'), s.count('1'), s.count('2'))
(z1, o1, t1) = (0, 0, 0)
for i in range(n):
	if z == o == t == 0:
		break
	if s[i] == '0':
		z1 += 1
		if z <= 0:
			continue
	elif s[i] == '1':
		o1 += 1
		if o <= 0:
			continue
	else:
		if t <= 0:
			continue
		t1 += 1
	if z < 0:
		repl = '0'
	elif o < 0:
		repl = '1'
	elif t < 0:
		repl = '2'
	if int(repl) < int(s[i]):
		if s[i] == '0':
			z -= 1
		elif s[i] == '1':
			o -= 1
		elif s[i] == '2':
			t -= 1
		if repl == '0':
			z += 1
		elif repl == '1':
			o += 1
		else:
			t += 1
		s[i] = repl
	elif int(repl) > int(s[i]):
		if repl == '1':
			if s[i] == '0':
				if zcnt - z1 < z:
					s[i] = repl
					z -= 1
					o += 1
		else:
			if s[i] == '0':
				if zcnt - z1 < z:
					s[i] = repl
					z -= 1
					t += 1
			if s[i] == '1':
				if ocnt - o1 < o:
					s[i] = repl
					o -= 1
					t += 1
print(''.join(s))


import sys
import math

def read_int():
	return int(input().strip())

def read_int_list():
	return list(map(int, input().strip().split()))

def read_string():
	return input().strip()

def read_string_list(delim=' '):
	return input().strip().split(delim)
n = read_int()
inp = list(map(lambda x: int(x), list(read_string())))
out = [-1 for _ in range(n)]
counter = [0, 0, 0]
for i in inp:
	counter[i] += 1
for i in range(3):
	counter[i] -= n // 3
start = 0
while start < n and counter[2] > 0:
	if inp[start] == 2:
		if counter[0] < 0:
			counter[2] -= 1
			out[start] = 0
			counter[0] += 1
			start += 1
			continue
		if counter[1] < 0:
			counter[2] -= 1
			out[start] = 1
			counter[1] += 1
	start += 1
start = 0
while start < n and counter[1] > 0:
	if inp[start] == 1:
		if counter[0] < 0:
			counter[1] -= 1
			out[start] = 0
			counter[0] += 1
	start += 1
end = n - 1
while end > -1 and counter[0] > 0:
	if inp[end] == 0:
		if counter[2] < 0:
			counter[0] -= 1
			out[end] = 2
			counter[2] += 1
			end -= 1
			continue
		if counter[1] < 0:
			counter[0] -= 1
			out[end] = 1
			counter[1] += 1
	end -= 1
end = n - 1
while end > -1 and counter[1] > 0:
	if inp[end] == 1:
		if counter[2] < 0:
			out[end] = 2
			counter[2] += 1
			counter[1] -= 1
	end -= 1
for i in range(n):
	if out[i] == -1:
		out[i] = inp[i]
print(''.join(list(map(lambda x: str(x), out))))


n = int(input())
s = input()
c = {x: n // 3 - s.count(x) for x in ['2', '1', '0']}
if c['0'] < 0:
	if c['2'] > 0:
		r = min(-c['0'], c['2'])
		c['0'] += r
		c['2'] -= r
		s = s[::-1].replace('0', '2', r)[::-1]
	if c['1'] > 0:
		r = min(-c['0'], c['1'])
		c['0'] += r
		c['1'] -= r
		s = s[::-1].replace('0', '1', r)[::-1]
if c['2'] < 0:
	if c['0'] > 0:
		r = min(c['0'], -c['2'])
		c['0'] -= r
		c['2'] += r
		s = s.replace('2', '0', r)
	if c['1'] > 0:
		r = min(c['1'], -c['2'])
		c['1'] -= r
		c['2'] += r
		s = s.replace('2', '1', r)
if c['1'] < 0:
	if c['0'] > 0:
		r = min(-c['1'], c['0'])
		c['1'] += r
		c['0'] -= r
		s = s.replace('1', '0', r)
	if c['2'] > 0:
		r = min(-c['1'], c['2'])
		c['1'] += r
		c['2'] -= r
		s = s[::-1].replace('1', '2', r)[::-1]
print(s)


n = int(input())
s = [ord(x) - ord('0') for x in input()]
cnt = [s.count(x) for x in [0, 1, 2]]

def forw(x):
	for i in range(n):
		if cnt[x] < n // 3 and s[i] > x and (cnt[s[i]] > n // 3):
			cnt[x] += 1
			cnt[s[i]] -= 1
			s[i] = x

def back(x):
	for i in range(n - 1, -1, -1):
		if cnt[x] < n // 3 and s[i] < x and (cnt[s[i]] > n // 3):
			cnt[x] += 1
			cnt[s[i]] -= 1
			s[i] = x
forw(0)
forw(1)
back(2)
back(1)
print(''.join([str(x) for x in s]))


from collections import defaultdict
n = int(input())
a = list(map(int, input().strip()))
d = defaultdict(int)
for i in a:
	d[i] += 1
for i in range(n):
	if a[i] == 2 and d[2] > n // 3:
		if d[0] < n // 3:
			a[i] = 0
			d[0] += 1
			d[2] -= 1
		elif d[1] < n // 3:
			a[i] = 1
			d[1] += 1
			d[2] -= 1
	elif a[i] == 1 and d[1] > n // 3:
		if d[0] < n // 3:
			a[i] = 0
			d[0] += 1
			d[1] -= 1
for i in range(n - 1, -1, -1):
	if a[i] == 0 and d[0] > n // 3:
		if d[2] < n // 3:
			a[i] = 2
			d[2] += 1
			d[0] -= 1
		elif d[1] < n // 3:
			a[i] = 1
			d[0] -= 1
			d[1] += 1
	elif a[i] == 1 and d[1] > n // 3:
		if d[2] < n // 3:
			a[i] = 2
			d[2] += 1
			d[1] -= 1
print(''.join(map(str, a)))


from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt, sin, cos, ceil, floor
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(1000000000)
INF = float('inf')
(YES, Yes, yes, NO, No, no) = ('YES', 'Yes', 'yes', 'NO', 'No', 'no')
(dy4, dx4) = ([0, 1, 0, -1], [1, 0, -1, 0])
(dy8, dx8) = ([0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1])

def inside(y, x, H, W):
	return 0 <= y < H and 0 <= x < W

def ceil(a, b):
	return (a + b - 1) // b

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	g = gcd(a, b)
	return a / g * b

def euclidean_distance(y1, x1, y2, x2):
	return sqrt((y1 - y2) ** 2 + (x1 - x2) ** 2)

def manhattan_distance(y1, x1, y2, x2):
	return abs(y1 - y2) + abs(x1 - x2)

def main():
	N = int(input())
	S = input()
	counter = Counter(S)
	num = N // 3
	ans = [''] * N
	for (i, c) in enumerate(S):
		if counter[c] <= num:
			ans[i] = c
	for a in '012':
		if counter[a] < num:
			for (i, c) in enumerate(S):
				if ans[i] == '' and counter[a] < num and (counter[c] > num):
					ans[i] = a
					counter[c] -= 1
					counter[a] += 1
		elif counter[a] > num:
			x = num
			for (i, c) in enumerate(S):
				if ans[i] == '' and c == a and (x > 0):
					ans[i] = c
				x -= ans[i] == a
		else:
			for (i, c) in enumerate(S):
				if ans[i] == '' and c == a:
					ans[i] = c
	print(''.join(ans))
main()


n = int(input())
s = list(input())
(a, b, c) = (0, 0, 0)
for (i, x) in enumerate(s):
	if x == '0':
		a += 1
	elif x == '1':
		b += 1
	else:
		c += 1
norma = n // 3
for (i, x) in enumerate(s):
	if c <= norma:
		break
	if x == '2':
		if a < norma:
			s[i] = '0'
			a += 1
		elif b < norma:
			s[i] = '1'
			b += 1
		else:
			break
		c -= 1
for (i, x) in enumerate(s):
	if b <= norma:
		break
	if x == '1':
		if a < norma:
			s[i] = '0'
			a += 1
		else:
			break
		b -= 1
for i in range(n - 1, -1, -1):
	x = s[i]
	if b <= norma:
		break
	if x == '1':
		if c < norma:
			s[i] = '2'
			c += 1
		else:
			break
		b -= 1
for i in range(n - 1, -1, -1):
	x = s[i]
	if a <= norma:
		break
	if x == '0':
		if c < norma:
			s[i] = '2'
			c += 1
		elif b < norma:
			s[i] = '1'
			b += 1
		else:
			break
		a -= 1
print(''.join(s))


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import sys
n = int(input())
s = list(input())
a = 0
b = 0
c = 0
for i in range(n):
	if s[i] == '1':
		b += 1
	elif s[i] == '0':
		a += 1
	else:
		c += 1
if a == b == c:
	print(*s, sep='')
	sys.exit()
else:
	if a < n // 3:
		for i in range(n):
			if s[i] == '1' and b > n // 3:
				s[i] = '0'
				a += 1
				b -= 1
			elif s[i] == '2' and c > n // 3:
				s[i] = '0'
				a += 1
				c -= 1
			if a == n // 3:
				break
	if b < n // 3:
		co = 0
		for i in range(n):
			if s[i] == '0':
				co += 1
				if co > n // 3:
					a -= 1
					b += 1
					s[i] = '1'
			elif s[i] == '2' and c > n // 3:
				s[i] = '1'
				b += 1
				c -= 1
			if b == n // 3:
				break
	if c < n // 3:
		for i in range(n - 1, -1, -1):
			if s[i] == '0' and a > n // 3:
				s[i] = '2'
				a -= 1
				c += 1
			elif s[i] == '1' and b > n // 3:
				s[i] = '2'
				b -= 1
				c += 1
			if c == n // 3:
				break
	print(*s, sep='')


n = int(input())
s = input()
(c0, c1, c2) = (0, 0, 0)
for x in s:
	if x == '0':
		c0 += 1
	elif x == '1':
		c1 += 1
	elif x == '2':
		c2 += 1
r = n // 3
need = abs(r - c0) + abs(r - c1) + abs(r - c2)
need //= 2
ar = [x for x in s]
if c0 < r:
	for x in range(n):
		if ar[x] == '1' and c1 > r:
			c0 += 1
			c1 -= 1
			ar[x] = '0'
		elif ar[x] == '2' and c2 > r:
			c0 += 1
			c2 -= 1
			ar[x] = '0'
		if c0 == r:
			break
if c2 < r:
	for x in range(n - 1, -1, -1):
		if ar[x] == '1' and c1 > r:
			c2 += 1
			c1 -= 1
			ar[x] = '2'
		elif ar[x] == '0' and c0 > r:
			c2 += 1
			c0 -= 1
			ar[x] = '2'
		if c2 == r:
			break
if c2 > r:
	for x in range(n):
		if ar[x] == '2':
			c1 += 1
			c2 -= 1
			ar[x] = '1'
		if c2 == r:
			break
if c0 > r:
	for x in range(n - 1, -1, -1):
		if ar[x] == '0':
			c1 += 1
			c0 -= 1
			ar[x] = '1'
		if c0 == r:
			break
print(''.join(ar))


def main():
	n = int(input())
	ternary_string = input()
	balance_num = n // 3
	count = [ternary_string.count(x) for x in '012']
	for x in [0, 2, 1]:
		for y in range(3):
			if count[x] < balance_num < count[y]:
				change_num = min(balance_num - count[x], count[y] - balance_num)
				if x > y:
					ternary_string = ternary_string[::-1].replace(str(y), str(x), change_num)[::-1]
				else:
					ternary_string = ternary_string.replace(str(y), str(x), change_num)
	print(ternary_string)
main()


import math, sys
from sys import stdin, stdout
from collections import Counter, defaultdict, deque
input = stdin.readline
I = lambda : int(input())
li = lambda : list(map(int, input().split()))

def case():
	n = int(input())
	a = input().strip()
	a = list(a)
	(zero, one, two) = (0, 0, 0)
	for i in range(n):
		if a[i] == '0':
			zero += 1
		elif a[i] == '1':
			one += 1
		else:
			two += 1
	for i in range(n):
		if a[i] == '1' and one > n // 3 and (zero < n // 3):
			a[i] = '0'
			one -= 1
			zero += 1
		elif a[i] == '2':
			if two > n // 3 and zero < n // 3:
				a[i] = '0'
				two -= 1
				zero += 1
			elif two > n // 3 and one < n // 3:
				a[i] = '1'
				one += 1
				two -= 1
	for i in range(1, n + 1):
		if a[-i] == '0':
			if zero > n // 3 and two < n // 3:
				a[-i] = '2'
				two += 1
				zero -= 1
			elif zero > n // 3 and one < n // 3:
				a[-i] = '1'
				zero -= 1
				one += 1
		elif a[-i] == '1':
			if one > n // 3 and two < n // 3:
				a[-i] = '2'
				one -= 1
				two += 1
	print(''.join(map(str, a)))
for _ in range(1):
	case()


def main():
	(n, l) = (int(input()) // 3, list(input()))
	(cnt, i) = ({c: l.count(c) for c in '012'}, 0)
	while cnt['0'] < n:
		c = l[i]
		if cnt[c] > n:
			cnt[c] -= 1
			cnt['0'] += 1
			l[i] = '0'
		i += 1
	while cnt['1'] < n < cnt['2']:
		if l[i] == '2':
			l[i] = '1'
			cnt['2'] -= 1
			cnt['1'] += 1
		i += 1
	i = n * 3 - 1
	while cnt['2'] < n:
		c = l[i]
		if cnt[c] > n:
			cnt[c] -= 1
			cnt['2'] += 1
			l[i] = '2'
		i -= 1
	while cnt['1'] < n < cnt['0']:
		if l[i] == '0':
			l[i] = '1'
			cnt['0'] -= 1
			cnt['1'] += 1
		i -= 1
	print(''.join(l))
main()


from collections import Counter
n = int(input())
string = input()
c = Counter(string)
n0 = c['0'] - n // 3
n1 = c['1'] - n // 3
n2 = c['2'] - n // 3

def replace(l, a, b, num):
	if a > b:
		cnt = 0
		for i in range(n):
			if l[i] == a:
				cnt += 1
				l[i] = b
				if cnt == num:
					break
	else:
		cnt = 0
		for i in range(n - 1, -1, -1):
			if l[i] == a:
				cnt += 1
				l[i] = b
				if cnt == num:
					break
	return
if n0 == n1 == 0:
	print(string)
else:
	string = list(string)
	if n0 == 0:
		if n1 > 0:
			replace(string, '1', '2', n1)
		else:
			replace(string, '2', '1', n2)
	elif n1 == 0:
		if n0 > 0:
			replace(string, '0', '2', n0)
		else:
			replace(string, '2', '0', n2)
	elif n2 == 0:
		if n0 > 0:
			replace(string, '0', '1', n0)
		else:
			replace(string, '1', '0', n1)
	elif n0 > 0 and n1 > 0 and (n2 < 0):
		replace(string, '0', '2', n0)
		replace(string, '1', '2', n1)
	elif n1 > 0 and n2 > 0 and (n0 < 0):
		replace(string, '1', '0', n1)
		replace(string, '2', '0', n2)
	elif n2 > 0 and n0 > 0 and (n1 < 0):
		replace(string, '2', '1', n2)
		replace(string, '0', '1', n0)
	elif n0 > 0 and n1 < 0 and (n2 < 0):
		replace(string, '0', '2', -n2)
		replace(string, '0', '1', -n1)
	elif n1 > 0 and n2 < 0 and (n0 < 0):
		replace(string, '1', '0', -n0)
		replace(string, '1', '2', -n2)
	elif n2 > 0 and n1 < 0 and (n0 < 0):
		replace(string, '2', '0', -n0)
		replace(string, '2', '1', -n1)
	print(''.join(string))


n = int(input())
s = [ord(x) - ord('0') for x in input()]
cnt = [s.count(x) for x in [0, 1, 2]]

def Front(x):
	global s, cnt, n
	for i in range(n):
		if cnt[x] < n // 3 and s[i] > x and (cnt[s[i]] > n // 3):
			cnt[s[i]] -= 1
			cnt[x] += 1
			s[i] = x

def Back(x):
	global s, cnt, n
	for i in range(n - 1, -1, -1):
		if cnt[x] < n // 3 and s[i] < x and (cnt[s[i]] > n // 3):
			cnt[s[i]] -= 1
			cnt[x] += 1
			s[i] = x
Front(0)
Front(1)
Back(2)
Back(1)
print(''.join([str(x) for x in s]))


n = int(input())
s = list(input())
d = {'0': 0, '1': 0, '2': 0}
for i in range(n):
	d[s[i]] += 1
f = n // 3
for i in range(n):
	if d[s[i]] > f and d['0'] < f:
		d[s[i]] -= 1
		s[i] = '0'
		d[s[i]] += 1
for i in range(n - 1, -1, -1):
	if d[s[i]] > f and d['2'] < f:
		d[s[i]] -= 1
		s[i] = '2'
		d[s[i]] += 1
for i in range(n - 1, -1, -1):
	if s[i] == '0' and d[s[i]] > f and (d['1'] < f):
		d[s[i]] -= 1
		s[i] = '1'
		d[s[i]] += 1
for i in range(n):
	if s[i] == '2' and d[s[i]] > f and (d['1'] < f):
		d[s[i]] -= 1
		s[i] = '1'
		d[s[i]] += 1
print(''.join(s))


from sys import exit
from collections import Counter
n = int(input())
str = list(input())
mp = Counter(str)
(z, o, t) = (mp['0'], mp['1'], mp['2'])
m = n // 3
if t < m:
	for i in range(n - 1, -1, -1):
		if str[i] == '0' and z > m:
			z -= 1
			str[i] = '2'
			t += 1
		elif str[i] == '1' and o > m:
			o -= 1
			str[i] = '2'
			t += 1
		if t == m:
			break
elif t > m:
	for i in range(n):
		if str[i] == '2' and z < m:
			z += 1
			str[i] = '0'
			t -= 1
		elif str[i] == '2' and o < m:
			o += 1
			str[i] = '1'
			t -= 1
		if t == m:
			break
if o < m:
	for i in range(n - 1, -1, -1):
		if str[i] == '0' and z > m:
			z -= 1
			str[i] = '1'
			o += 1
		if o == m:
			break
elif o > m:
	for i in range(n):
		if str[i] == '1' and z < m:
			z += 1
			str[i] = '0'
			o -= 1
		if o == m:
			break
print(''.join(str))


from collections import Counter
n = int(input())
a = list(input())
c = Counter(a)
d = {}
for v in '012':
	d[v] = c[v] - n // 3
i = 0
while d['0'] < 0:
	if a[i] != '0' and d[a[i]] > 0:
		d['0'] += 1
		d[a[i]] -= 1
		a[i] = '0'
	i += 1
i = n - 1
while d['0'] > 0:
	if a[i] == '0':
		if d['2'] < 0:
			a[i] = '2'
			d[a[i]] += 1
		else:
			a[i] = '1'
			d[a[i]] += 1
		d['0'] -= 1
	i -= 1
i = 0
while d['1'] < 0:
	if a[i] != '1' and d[a[i]] > 0:
		d['1'] += 1
		d[a[i]] -= 1
		a[i] = '1'
	i += 1
i = n - 1
while d['1'] > 0:
	if a[i] == '1':
		a[i] = '2'
		d[a[i]] += 1
		d['1'] -= 1
	i -= 1
print(''.join(a))


n = int(input())
s = input()
c = [0, 0, 0]
for i in s:
	c[int(i)] += 1
tot = n // 3
til = [0, 0, 0]
ans = [s[i] for i in range(n)]
dont = []
for i in range(n - 1, -1, -1):
	if s[i] == '0':
		if c[0] > tot:
			if c[2] < tot:
				ans[i] = '2'
				c[2] += 1
				c[0] -= 1
			else:
				ans[i] = '1'
				c[1] += 1
				c[0] -= 1
		else:
			ans[i] = '0'
for i in range(n):
	if s[i] == '1':
		if c[1] > tot:
			if c[0] < tot:
				ans[i] = '0'
				c[0] += 1
				c[1] -= 1
for i in range(n - 1, -1, -1):
	if s[i] == '1':
		if c[1] > tot:
			if c[2] < tot:
				ans[i] = '2'
				c[2] += 1
				c[1] -= 1
for i in range(n):
	if s[i] == '2':
		if c[2] > tot:
			if c[0] < tot:
				ans[i] = '0'
				c[0] += 1
				c[2] -= 1
			else:
				ans[i] = '1'
				c[1] += 1
				c[2] -= 1
		else:
			ans[i] = '2'
print(''.join(ans))


(n, s) = (int(input()) // 3, list(input()))
(prev, c) = ('', [s.count(ch) - n for ch in '012'])
for (ch1, ch2) in zip(['0'] * c[0] + ['2'] * c[2] + ['1'] * c[1], ['0'] * -c[0] + ['2'] * -c[2] + ['1'] * -c[1]):
	if ch1 != prev:
		(i, j) = (0, -1)
	prev = ch1
	if ch1 > ch2:
		while s[i] != ch1:
			i += 1
		s[i] = ch2
	else:
		while s[j] != ch1:
			j -= 1
		s[j] = ch2
print(''.join(s))


n = int(input())
s = input()
a = []
(k0, k1, k2) = (0, 0, 0)
for i in s:
	if i == '0':
		k0 += 1
	elif i == '1':
		k1 += 1
	else:
		k2 += 1
	a.append(i)
n1 = n // 3
if k1 == k2 and k2 == k0:
	print(s)
else:
	for i in range(n):
		if s[i] == '2':
			if k2 > n1:
				if k0 < n1:
					a[i] = '0'
					k0 += 1
					k2 -= 1
				elif k1 < n1:
					a[i] = '1'
					k1 += 1
					k2 -= 1
		if s[i] == '1':
			if k1 > n1:
				if k0 < n1:
					a[i] = '0'
					k0 += 1
					k1 -= 1
	for i in range(n - 1, -1, -1):
		if a[i] == '0':
			if k0 > n1:
				if k2 < n1:
					a[i] = '2'
					k2 += 1
					k0 -= 1
				elif k1 < n1:
					a[i] = '1'
					k1 += 1
					k0 -= 1
		if a[i] == '1':
			if k1 > n1:
				if k2 < n1:
					a[i] = '2'
					k2 += 1
					k1 -= 1
	for i in a:
		print(i, end='')


from collections import deque
(n, s) = (int(input()), list(map(int, list(input()))))
l = n // 3
pos = [deque(), deque(), deque()]
for i in range(n):
	pos[s[i]].append(i)
t = []
for i in range(3):
	t.append(l - len(pos[i]))
for i in [0, 2, 1]:
	for j in range(3):
		if i == j:
			continue
		while t[i] > 0 and t[j] < 0:
			if i < j:
				pos[i].append(pos[j].popleft())
			else:
				pos[i].append(pos[j].pop())
			t[i] -= 1
			t[j] += 1
	pos[i] = deque(list(sorted(pos[i])))
for i in range(3):
	for j in pos[i]:
		s[j] = i
print(''.join(map(str, s)))


def solve(n, s):
	counter = {'0': 0, '1': 0, '2': 0}
	remaining = {'0': 0, '1': 0, '2': 0}
	result = []
	for index in range(n):
		char = s[index]
		counter[char] += 1
		result.append(char)
	target_char_count = n // 3
	keys = ['0', '1', '2']
	for index in range(n):
		index_count = 0
		while index_count < 3 and counter[result[index]] > target_char_count:
			key = keys[index_count]
			if counter[key] < target_char_count:
				if key > result[index] and remaining[result[index]] < target_char_count:
					break
				counter[result[index]] -= 1
				counter[key] += 1
				result[index] = key
			index_count += 1
		remaining[result[index]] += 1
	return ''.join(result)
n = int(input().strip())
s = input().strip()
print(solve(n, s))


from math import *
from collections import deque
from sys import stdin, stdout, setrecursionlimit
input = lambda : stdin.readline().strip()
print = stdout.write
n = int(input())
s = input()
req = n // 3
d = {}
for i in range(len(s)):
	key = int(s[i])
	if key in d:
		d[key].append(i)
	else:
		d[key] = deque([i])
(a, b, c) = (0, 0, 0)
if 0 in d:
	a = len(d[0])
else:
	d[0] = deque([])
if 1 in d:
	b = len(d[1])
else:
	d[1] = deque([])
if 2 in d:
	c = len(d[2])
else:
	d[2] = deque([])
if c < req:
	if b > req:
		for i in range(req - c):
			if b - 1 < req:
				break
			else:
				d[2].append(d[1].pop())
				b -= 1
				c += 1
	if c < req:
		if a > req:
			for i in range(req - c):
				if a - 1 < req:
					break
				else:
					d[2].append(d[0].pop())
					a -= 1
					c += 1
if a < req:
	if b > req:
		for i in range(req - a):
			if b - 1 < req:
				break
			else:
				d[0].append(d[1].popleft())
				b -= 1
				a += 1
	if a < req:
		if c > req:
			for i in range(req - a):
				if c - 1 < req:
					break
				else:
					d[0].append(d[2].popleft())
					c -= 1
					a += 1
if b < req:
	if a > req:
		for i in range(req - b):
			if a - 1 < req:
				break
			else:
				d[1].append(d[0].pop())
				b += 1
				a -= 1
	if b < req:
		if c > req:
			for i in range(req - b):
				if c - 1 < req:
					break
				else:
					d[1].append(d[2].popleft())
					c -= 1
					b += 1
ans = ['0'] * n
for i in d:
	x = list(d[i])
	for j in range(len(x)):
		ans[x[j]] = str(i)
ans = ''.join(ans)
print(str(ans))


n = int(input())
s = list(input())
(a0, a1, a2) = (0, 0, 0)
for i in s:
	if i == '0':
		a0 += 1
	elif i == '1':
		a1 += 1
	else:
		a2 += 1
for i in range(0, len(s)):
	if s[i] != '0' and a0 < n // 3:
		if s[i] == '1' and a1 > n // 3:
			a1 -= 1
			s[i] = '0'
			a0 += 1
		elif s[i] == '2' and a2 > n // 3:
			a2 -= 1
			s[i] = '0'
			a0 += 1
	elif a0 >= n // 3:
		break
for i in range(len(s) - 1, 0, -1):
	if s[i] != '2' and a2 < n // 3:
		if s[i] == '1' and a1 > n // 3:
			a1 -= 1
			a2 += 1
			s[i] = '2'
		elif s[i] == '0' and a0 > n // 3:
			a0 -= 1
			a2 += 1
			s[i] = '2'
	elif a2 >= n // 3:
		break
for i in range(0, len(s)):
	if s[i] == '2' and a1 < n // 3 and (a2 > n // 3):
		s[i] = '1'
		a1 += 1
		a2 -= 1
	elif a1 >= n // 3:
		break
for i in range(len(s) - 1, 0, -1):
	if s[i] == '0' and a1 < n // 3 and (a0 > n // 3):
		s[i] = '1'
		a1 += 1
		a0 -= 1
	elif a1 >= n // 3:
		break
print(''.join(s))


n = int(input())
y = [int(i) for i in input()]
count = [0, 0, 0]
norm = n / 3
for i in y:
	if i == 0:
		count[0] = count[0] + 1
	if i == 1:
		count[1] = count[1] + 1
	if i == 2:
		count[2] = count[2] + 1
if count[0] > norm:
	i = 0
	while count[2] < norm and count[0] > norm:
		if y[-i - 1] == 0:
			y[-i - 1] = 2
			count[2] = count[2] + 1
			count[0] = count[0] - 1
		i = i + 1
	while count[1] < norm and count[0] > norm:
		if y[-i - 1] == 0:
			y[-i - 1] = 1
			count[1] = count[1] + 1
			count[0] = count[0] - 1
		i = i + 1
if count[1] > norm:
	i = 0
	while count[0] < norm and count[1] > norm:
		if y[i] == 1:
			y[i] = 0
			count[0] = count[0] + 1
			count[1] = count[1] - 1
		i = i + 1
	i = 0
	while count[2] < norm and count[1] > norm:
		if y[-i - 1] == 1:
			y[-i - 1] = 2
			count[2] = count[2] + 1
			count[1] = count[1] - 1
		i = i + 1
if count[2] > norm:
	i = 0
	while count[0] < norm and count[2] > norm:
		if y[i] == 2:
			y[i] = 0
			count[0] = count[0] + 1
			count[2] = count[2] - 1
		i = i + 1
	while count[1] < norm and count[2] > norm:
		if y[i] == 2:
			y[i] = 1
			count[1] = count[1] + 1
			count[2] = count[2] - 1
		i = i + 1
result = ''.join((str(x) for x in y))
print(result)


from os import path
from io import BytesIO, IOBase
import sys
from heapq import heappush, heappop
from functools import cmp_to_key as ctk
from collections import deque, Counter, defaultdict as dd
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from itertools import permutations
from datetime import datetime
from math import ceil, sqrt, log, gcd

def ii():
	return int(input())

def si():
	return input().rstrip()

def mi():
	return map(int, input().split())

def li():
	return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
mod = 998244353
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def bo(i):
	return ord(i) - ord('0')
file = 1

def ceil(a, b):
	return (a + b - 1) // b

def solve():
	n = ii()
	s = list(si())
	req = n // 3
	x = s.count('0')
	y = s.count('1')
	z = s.count('2')
	if x < req:
		x1 = y - req
		x2 = z - req
		need = req - x
		for i in range(n):
			if need <= 0:
				break
			if s[i] == '1' and x1 > 0:
				s[i] = '0'
				x1 -= 1
				y -= 1
				need -= 1
			elif s[i] == '2' and x2 > 0:
				s[i] = '0'
				x2 -= 1
				z -= 1
				need -= 1
		x = req
	if y < req:
		x1 = x - req
		x2 = z - req
		need = req - y
		if x2 > 0:
			for i in range(n):
				if need <= 0:
					break
				if s[i] == '2' and x2 > 0:
					x2 -= 1
					s[i] = '1'
					y += 1
					z -= 1
					need -= 1
		if x1 > 0:
			for i in range(n - 1, -1, -1):
				if s[i] == '0' and x2 < 0:
					s[i] = '2'
					x2 += 1
					z += 1
					x -= 1
			for i in range(n - 1, -1, -1):
				if need <= 0:
					break
				if s[i] == '0':
					s[i] = '1'
					need -= 1
					y += 1
					x -= 1
	if z < req:
		x1 = x - req
		x2 = y - req
		need = req - z
		for i in range(n - 1, -1, -1):
			if need <= 0:
				break
			if s[i] == '0' and x1 > 0:
				x1 -= 1
				s[i] = '2'
				need -= 1
			if s[i] == '1' and x2 > 0:
				x2 -= 1
				s[i] = '2'
				need -= 1
	print(''.join(s))
if file:
	if path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	else:
		input = sys.stdin.readline
solve()


n = int(input())
s = list(str(input()))
(c0, c1, c2) = (0, 0, 0)
C0 = []
C1 = []
C2 = []
for i in range(n):
	if s[i] == '0':
		c0 += 1
		C0.append(i)
	elif s[i] == '1':
		c1 += 1
		C1.append(i)
	else:
		c2 += 1
		C2.append(i)
import copy
if c0 > n // 3:
	if c1 > n // 3:
		ans = copy.copy(s)
		for i in range(n // 3, c0):
			j = C0[i]
			ans[j] = '2'
		for i in range(n // 3, c1):
			j = C1[i]
			ans[j] = '2'
	elif c2 > n // 3:
		ans = copy.copy(s)
		for i in range(n // 3, c0):
			j = C0[i]
			ans[j] = '1'
		for i in range(c2 - n // 3):
			j = C2[i]
			ans[j] = '1'
	else:
		ans = copy.copy(s)
		temp = ['1'] * (n // 3 - c1) + ['2'] * (n // 3 - c2)
		for i in range(n // 3, c0):
			j = C0[i]
			ans[j] = temp[i - n // 3]
elif c1 > n // 3:
	if c2 > n // 3:
		ans = copy.copy(s)
		for i in range(c1 - n // 3):
			j = C1[i]
			ans[j] = '0'
		for i in range(c2 - n // 3):
			j = C2[i]
			ans[j] = '0'
	else:
		ans = copy.copy(s)
		for i in range(n // 3 - c0):
			j = C1[i]
			ans[j] = '0'
		for i in range(c1 - (n // 3 - c2), c1):
			j = C1[i]
			ans[j] = '2'
elif c2 > n // 3:
	ans = copy.copy(s)
	temp = ['0'] * (n // 3 - c0) + ['1'] * (n // 3 - c1)
	for i in range(c2 - n // 3):
		j = C2[i]
		ans[j] = temp[i]
else:
	ans = copy.copy(s)
print(''.join(ans))


from sys import maxsize, stdout, stdin, stderr
mod = int(1000000000.0 + 7)
import sys

def I():
	return int(stdin.readline())

def lint():
	return [int(x) for x in stdin.readline().split()]

def S():
	return list(map(str, input().strip()))

def grid(r, c):
	return [lint() for i in range(r)]
from collections import defaultdict, Counter, deque
import math
import heapq
from heapq import heappop, heappush
import bisect
from itertools import groupby
from itertools import permutations as comb

def gcd(a, b):
	while b:
		a %= b
		tmp = a
		a = b
		b = tmp
	return a

def lcm(a, b):
	return a // gcd(a, b) * b

def check_prime(n):
	for i in range(2, int(n ** (1 / 2)) + 1):
		if not n % i:
			return False
	return True

def nCr(n, r):
	return fact(n) // (fact(r) * fact(n - r))

def fact(n):
	res = 1
	for i in range(2, n + 1):
		res = res * i
	return res

def primefactors(n):
	num = 0
	while n % 2 == 0:
		num += 1
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			num += 1
			n = n // i
	if n > 2:
		num += 1
	return num

def ask(a):
	print('? {}'.format(a), flush=True)
	n = I()
	return n

def dfs(i, p):
	(a, tmp) = (0, 0)
	for j in d[i]:
		if j != p:
			a += 1
			tmp += dfs(j, i)
	if a == 0:
		return 0
	return tmp / a + 1

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(i)
			n = n // i
	if n > 2:
		l.append(n)
	return l
n = I()
s = S()
c = ['2'] * n
d = Counter(s)
d1 = Counter(c)
for i in range(2):
	cnt = 0
	for j in range(n):
		if cnt == n // 3:
			break
		elif s[j] == str(i):
			cnt += 1
			c[j] = str(i)
		elif d[str(i)] < n // 3:
			if d[s[j]] > n // 3 and (s[j] != c[j] or int(s[j]) > i):
				print()
				d[s[j]] -= 1
				d[str(i)] += 1
				c[j] = str(i)
				s[j] = str(i)
				cnt += 1
			else:
				c[j] = s[j]
print(''.join(c))


n = int(input())
s = input()
count0 = 0
count1 = 0
count2 = 0
l = list(s)
for i in range(n):
	if s[i] == '0':
		count0 += 1
	elif s[i] == '1':
		count1 += 1
	else:
		count2 += 1
ideal = n // 3
diff0 = count0 - ideal
diff1 = count1 - ideal
diff2 = count2 - ideal
if diff0 < 0:
	for i in range(n):
		if diff0 < 0:
			if diff2 > 0 and l[i] == '2':
				l[i] = '0'
				diff0 += 1
				diff2 -= 1
			elif diff1 > 0 and l[i] == '1':
				l[i] = '0'
				diff0 += 1
				diff1 -= 1
		else:
			break
elif diff0 > 0:
	for i in range(n - 1, -1, -1):
		if diff0 > 0 and l[i] == '0':
			if diff2 < 0:
				l[i] = '2'
				diff0 -= 1
				diff2 += 1
			elif diff1 < 0:
				l[i] = '1'
				diff0 -= 1
				diff1 += 1
if diff0 == 0:
	if diff1 > diff2:
		for i in range(n - 1, -1, -1):
			if diff1 > diff2:
				if l[i] == '1':
					l[i] = '2'
					diff1 -= 1
					diff2 += 1
			else:
				break
	else:
		for i in range(n):
			if diff1 < diff2:
				if l[i] == '2':
					l[i] = '1'
					diff1 += 1
					diff2 -= 1
			else:
				break
print(''.join(l))


def replace(f: int, t: int, c: int):
	if c == 0:
		return
	global s
	tc = c
	if t < f:
		for i in range(n):
			if tc == 0:
				break
			if s[i] == f:
				s[i] = t
				tc -= 1
	else:
		for i in range(n - 1, -1, -1):
			if tc == 0:
				break
			if s[i] == f:
				s[i] = t
				tc -= 1
n = int(input())
s = [int(z) for z in input().strip()]
c = [0] * 3
for i in range(3):
	c[i] = s.count(i)
td = [0] * 3
for i in range(3):
	td[i] = n // 3 - c[i]
md = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
for f in range(3):
	for t in range(3):
		if td[f] < 0 and td[t] > 0:
			md[f][t] = min(abs(td[f]), abs(td[t]))
for (f, t) in ((0, 2), (2, 0), (0, 1), (1, 0), (1, 2), (2, 1)):
	replace(f, t, md[f][t])
print(*s, sep='')


n = input()
s = list(map(int, list(input())))
key = [0, 0, 0]
for digit in s:
	key[digit] += 1
for (index, digit) in enumerate(s):
	if key[digit] > len(s) // 3:
		if not digit:
			continue
		k = 0
		while key[k] >= len(s) // 3:
			k += 1
			if k == digit:
				break
		else:
			key[digit] -= 1
			key[k] += 1
			s[index] = k
for (index, digit) in enumerate(s[::-1]):
	if key[digit] > len(s) // 3:
		if digit == 2:
			continue
		k = 2
		while key[k] >= len(s) // 3:
			k -= 1
			if k == -1:
				break
		else:
			key[digit] -= 1
			key[k] += 1
			s[-index - 1] = k
print(''.join(map(str, s)))


n = int(input())
s = list(input())
m = n // 3
num = [0] * 3
for i in range(n):
	num[int(s[i])] += 1
cnt = []
for i in range(3):
	if num[i] < m:
		cnt.append(i)
if len(cnt) == 1:
	ind = cnt[0]
	for i in range(3):
		if i == ind:
			continue
		if i < ind:
			for j in range(n - 1, -1, -1):
				if num[i] == m:
					break
				if s[j] == str(i):
					s[j] = str(ind)
					num[i] -= 1
		else:
			for j in range(n):
				if num[i] == m:
					break
				if s[j] == str(i):
					s[j] = str(ind)
					num[i] -= 1
elif len(cnt) == 2:
	k = None
	for i in range(3):
		if i not in cnt:
			k = i
	if cnt == [1, 2]:
		cnt = [2, 1]
	for i in cnt:
		if k < i:
			for j in range(n - 1, -1, -1):
				if num[i] == m:
					break
				if s[j] == str(k):
					s[j] = str(i)
					num[i] += 1
		else:
			for j in range(n):
				if num[i] == m:
					break
				if s[j] == str(k):
					s[j] = str(i)
					num[i] += 1
print(''.join(s))


n = int(input())
s = list(input())
num = [0] * 3
for i in range(s.__len__()):
	num[int(s[i])] += 1
excess = [i - n / 3 for i in num]
for i in range(n):
	if excess[0] < 0:
		if excess[2] > 0:
			if s[i] == '2':
				s[i] = '0'
				excess[0] += 1
				excess[2] -= 1
				continue
		if excess[1] > 0:
			if s[i] == '1':
				s[i] = '0'
				excess[0] += 1
				excess[1] -= 1
	else:
		break
for j in range(n):
	i = n - 1 - j
	if excess[2] < 0:
		if excess[0] > 0:
			if s[i] == '0':
				s[i] = '2'
				excess[2] += 1
				excess[0] -= 1
				continue
		if excess[1] > 0:
			if s[i] == '1':
				s[i] = '2'
				excess[2] += 1
				excess[1] -= 1
	else:
		break
for i in range(n):
	if excess[2] > 0 > excess[1]:
		if s[i] == '2':
			s[i] = '1'
			excess[1] += 1
			excess[2] -= 1
for j in range(n):
	i = n - 1 - j
	if excess[0] > 0 > excess[1]:
		if s[i] == '0':
			s[i] = '1'
			excess[1] += 1
			excess[0] -= 1
print(''.join([str(i) for i in s]))


n = int(input())
s = str(input())
x = [s.count('0'), s.count('1'), s.count('2')]
t = n // 3
if x[0] == x[1] and x[1] == x[2]:
	print(s)
else:
	s = list(s)
	i = 0
	while x[0] < t and i < n:
		if x[int(s[i])] > t:
			x[int(s[i])] -= 1
			s[i] = '0'
			x[0] += 1
		i += 1
	i = n - 1
	while x[2] < t and i >= 0:
		if x[int(s[i])] > t:
			x[int(s[i])] -= 1
			s[i] = '2'
			x[2] += 1
		i -= 1
	if x[0] > t and x[2] > t:
		i = 0
		while x[1] < t and i < n and (x[2] > t):
			if s[i] == '2' and x[2] > t:
				x[2] -= 1
				x[1] += 1
				s[i] = '1'
			i += 1
		i = n - 1
		while x[1] < t and i >= 0:
			if s[i] == '0':
				x[0] -= 1
				x[1] += 1
				s[i] = '1'
			i -= 1
	elif x[0] > t:
		i = n - 1
		while x[1] < t and i >= 0:
			if s[i] == '0':
				x[0] -= 1
				x[1] += 1
				s[i] = '1'
			i -= 1
	elif x[2] > t:
		i = 0
		while x[1] < t and i < n:
			if s[i] == '2':
				x[2] -= 1
				x[1] += 1
				s[i] = '1'
			i += 1
	print(''.join(s))


n = int(input())
n = n // 3
s = input()
c0 = s.count('0')
c1 = s.count('1')
c2 = s.count('2')
a = min(c2 - n, n - c0)
if a < 0:
	a = 0
s = s.replace('2', '0', a)
c0 = s.count('0')
c1 = s.count('1')
c2 = s.count('2')
a = min(c2 - n, n - c1)
if a < 0:
	a = 0
s = s.replace('2', '1', a)
c0 = s.count('0')
c1 = s.count('1')
c2 = s.count('2')
a = min(c1 - n, n - c0)
if a < 0:
	a = 0
s = s.replace('1', '0', a)
c0 = s.count('0')
c1 = s.count('1')
c2 = s.count('2')
s = s[::-1]
a = min(c0 - n, n - c2)
if a < 0:
	a = 0
s = s.replace('0', '2', a)
c0 = s.count('0')
c1 = s.count('1')
c2 = s.count('2')
a = min(c0 - n, n - c1)
if a < 0:
	a = 0
s = s.replace('0', '1', a)
c0 = s.count('0')
c1 = s.count('1')
c2 = s.count('2')
a = min(c1 - n, n - c2)
if a < 0:
	a = 0
s = s.replace('1', '2', a)
print(s[::-1])


n = int(input())
s = input()
nrml = n // 3
elm_array = [nrml - s.count(str(i)) for i in range(3)]
for i in [0, 2, 1]:
	if elm_array[i] > 0:
		for j in range(3):
			if elm_array[j] < 0:
				to_replace = min(elm_array[i], -elm_array[j])
				elm_array[i] -= to_replace
				elm_array[j] += to_replace
				if i > j:
					s = s[::-1].replace(str(j), str(i), to_replace)[::-1]
				else:
					s = s.replace(str(j), str(i), to_replace)
print(s)


n = int(input())
l = list(map(int, input()))
a = l.count(0)
b = l.count(1)
c = l.count(2)
for i in range(n):
	if a < n // 3:
		if l[i] == 1:
			if b > n // 3:
				l[i] = 0
				a += 1
				b -= 1
		elif l[i] == 2:
			if c > n // 3:
				l[i] = 0
				a += 1
				c -= 1
	else:
		break
num1 = 0
n = len(l)
for i in range(n):
	if l[i] == 0:
		num1 += 1
	if b < n // 3:
		if l[i] == 0:
			if num1 > n // 3:
				l[i] = 1
				a -= 1
				b += 1
				num1 -= 1
		if l[i] == 2:
			if c > n // 3:
				l[i] = 1
				b += 1
				c -= 1
	else:
		break
for i in range(n - 1, -1, -1):
	if c < n // 3:
		if l[i] == 0:
			if a > n // 3:
				l[i] = 2
				a -= 1
				c += 1
		if l[i] == 1:
			if b > n // 3:
				l[i] = 2
				b -= 1
				c += 1
for i in range(n):
	print(l[i], end='')


n = int(input())
s = list(input())
d = {'0': 0, '1': 0, '2': 0}
for i in range(n):
	d[s[i]] += 1
d1 = d['0']
d2 = d['1']
d3 = d['2']
norm = n // 3
if d1 > norm:
	for i in reversed(range(n)):
		if d1 == norm:
			break
		if s[i] == '0' and d3 < norm:
			s[i] = '2'
			d3 += 1
			d1 -= 1
			continue
		if s[i] == '0' and d2 < norm:
			s[i] = '1'
			d2 += 1
			d1 -= 1
			continue
if d2 > norm:
	for i in range(n):
		if d2 == norm:
			break
		if s[i] == '1' and d1 < norm:
			s[i] = '0'
			d1 += 1
			d2 -= 1
			continue
if d2 > norm:
	for i in reversed(range(n)):
		if d2 == norm:
			break
		if s[i] == '1' and d3 < norm:
			s[i] = '2'
			d3 += 1
			d2 -= 1
			continue
if d3 > norm:
	for i in range(n):
		if d3 == norm:
			break
		if s[i] == '2' and d1 < norm:
			s[i] = '0'
			d1 += 1
			d3 -= 1
			continue
		if s[i] == '2' and d2 < norm:
			s[i] = '1'
			d2 += 1
			d3 -= 1
			continue
print(''.join(s))


n = int(input())
string = input()
string = list(string)
i = 0
zero = 0
one = 0
two = 0
h = 0
while i < len(string):
	if string[i] == '0':
		zero += 1
	elif string[i] == '1':
		one += 1
	else:
		two += 1
	i += 1
i = 0
while zero < n / 3:
	if string[i] == '2' and two > n / 3:
		string[i] = '0'
		zero += 1
		two -= 1
	if string[i] == '1' and one > n / 3:
		string[i] = '0'
		zero += 1
		one -= 1
	i += 1
i = 0
while one < n / 3:
	if string[i] == '2' and two > n / 3:
		string[i] = '1'
		one += 1
		two -= 1
	if string[len(string) - 1 - i] == '0' and zero > n / 3:
		if two < n / 3:
			h += 1
		if two >= n / 3 or h > n / 3 - two:
			string[len(string) - 1 - i] = '1'
			one += 1
			zero -= 1
	i += 1
i = 0
while two < n / 3:
	if string[len(string) - 1 - i] == '0' and zero > n / 3:
		string[len(string) - 1 - i] = '2'
		two += 1
		zero -= 1
	if string[len(string) - 1 - i] == '1' and one > n / 3:
		string[len(string) - 1 - i] = '2'
		two += 1
		one -= 1
	i += 1
i = 0
while i < len(string):
	print(string[i], end='')
	i += 1


input()
n = input()
(zero, one, two) = (0, 0, 0)
for i in n:
	if i == '0':
		zero += 1
	elif i == '1':
		one += 1
	else:
		two += 1
needzero = len(n) // 3 - zero
needone = len(n) // 3 - one
needtwo = len(n) // 3 - two
result = [''] * len(n)
for i in range(len(n)):
	if n[i] == '2' and needtwo < 0:
		if needzero > 0:
			needtwo += 1
			needzero -= 1
			result[i] = '0'
		elif needone > 0:
			needtwo += 1
			needone -= 1
			result[i] = '1'
	elif n[i] == '1' and needone < 0 and (needzero > 0):
		needone += 1
		needzero -= 1
		result[i] = '0'
for i in range(len(n) - 1, -1, -1):
	if result[i] == '':
		if n[i] == '2' and needtwo < 0:
			if needone > 0:
				result[i] = '1'
				needone -= 1
				needtwo += 1
			elif needzero > 0:
				result[i] = '0'
				needzero -= 1
				needtwo += 1
		elif n[i] == '1' and needone < 0:
			if needtwo > 0:
				result[i] = '2'
				needtwo -= 1
				needone += 1
			elif needzero > 0:
				result[i] = '0'
				needzero -= 1
				needone += 1
		elif n[i] == '0' and needzero < 0:
			if needtwo > 0:
				result[i] = '2'
				needtwo -= 1
				needzero += 1
			elif needone > 0:
				result[i] = '1'
				needone -= 1
				needzero += 1
		else:
			result[i] = n[i]
print(''.join(result))


n = int(input())
s = input()
num = [s.count(ch) - n // 3 for ch in '012']
for x in [0, 2, 1]:
	if num[x] < 0:
		for y in range(3):
			if num[y] > 0:
				u = min(-num[x], num[y])
				num[x] += u
				num[y] -= u
				if x > y:
					s = s[::-1].replace(str(y), str(x), u)[::-1]
				else:
					s = s.replace(str(y), str(x), u)
print(s)


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import *
from copy import *
from collections import deque
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for pp in range(t):
		solve(pp)

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def google(p):
	print('Case #' + str(p) + ': ', end='')

def lcm(a, b):
	return a * b // gcd(a, b)

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def solve(case):
	n = int(inp())
	s = list(inp())
	for i in range(len(s)):
		s[i] = int(s[i])
	zpos = []
	opos = []
	tpos = []
	cnt = [0] * 3
	for i in range(len(s)):
		cnt[s[i]] += 1
		if s[i] == 0:
			zpos.append(i)
		if s[i] == 1:
			opos.append(i)
		if s[i] == 2:
			tpos.append(i)
	if cnt[0] > n // 3:
		curr = len(zpos) - 1
		if cnt[2] < n // 3:
			while cnt[2] != n // 3 and cnt[0] > n // 3:
				s[zpos[curr]] = 2
				cnt[2] += 1
				cnt[0] -= 1
				curr -= 1
		if cnt[1] < n // 3:
			while cnt[1] != n // 3 and cnt[0] > n // 3:
				s[zpos[curr]] = 1
				cnt[1] += 1
				cnt[0] -= 1
				curr -= 1
	if cnt[1] > n // 3:
		if cnt[0] < n // 3:
			curr = 0
			while cnt[0] != n // 3 and cnt[1] > n // 3:
				s[opos[curr]] = 0
				cnt[1] -= 1
				cnt[0] += 1
				curr += 1
		if cnt[2] < n // 3:
			curr = len(opos) - 1
			while cnt[2] != n // 3 and cnt[1] > n // 3:
				s[opos[curr]] = 2
				cnt[1] -= 1
				cnt[2] += 1
				curr -= 1
	if cnt[2] > n // 3:
		curr = 0
		if cnt[0] < n // 3:
			while cnt[0] != n // 3 and cnt[2] > n // 3:
				s[tpos[curr]] = 0
				cnt[0] += 1
				cnt[2] -= 1
				curr += 1
		if cnt[1] < n // 3:
			while cnt[1] != n // 3 and cnt[2] > n // 3:
				s[tpos[curr]] = 1
				cnt[1] += 1
				cnt[2] -= 1
				curr += 1
	print(''.join((str(s[i]) for i in range(len(s)))))
testcase(1)


n = int(input())
s = list(input())
ave = n // 3
(c0, c1, c2) = (0, 0, 0)
for c in s:
	if c == '0':
		c0 += 1
	if c == '1':
		c1 += 1
	if c == '2':
		c2 += 1
for check0 in range(len(s)):
	if s[check0] == '1' and c0 < ave and (c1 > ave):
		s[check0] = '0'
		c0 += 1
		c1 -= 1
	elif s[check0] == '2' and c0 < ave and (c2 > ave):
		s[check0] = '0'
		c0 += 1
		c2 -= 1
for check1 in range(len(s)):
	if s[check1] == '2' and c1 < ave and (c2 > ave):
		s[check1] = '1'
		c1 += 1
		c2 -= 1
for check2 in range(len(s) - 1, -1, -1):
	if s[check2] == '1' and c2 < ave and (c1 > ave):
		s[check2] = '2'
		c2 += 1
		c1 -= 1
	elif s[check2] == '0' and c2 < ave and (c0 > ave):
		s[check2] = '2'
		c2 += 1
		c0 -= 1
for check1 in range(len(s) - 1, -1, -1):
	if s[check1] == '0' and c1 < ave and (c0 > ave):
		s[check1] = '1'
		c1 += 1
		c0 -= 1
print(''.join(s))


n = int(input())
seq = list(input())
a = seq.count('0') - n // 3
b = seq.count('1') - n // 3
c = seq.count('2') - n // 3
if a < 0:
	i = 0
	while b > 0 and a < 0:
		if seq[i] == '1':
			seq[i] = '0'
			b -= 1
			a += 1
		i += 1
	i = 0
	while c > 0 and a < 0:
		if seq[i] == '2':
			seq[i] = '0'
			c -= 1
			a += 1
		i += 1
if b < 0:
	i = 0
	while c > 0 and b < 0:
		if seq[i] == '2':
			seq[i] = '1'
			c -= 1
			b += 1
		i += 1
if c < 0:
	i = len(seq) - 1
	while a > 0 and c < 0:
		if seq[i] == '0':
			seq[i] = '2'
			a -= 1
			c += 1
		i -= 1
	i = len(seq) - 1
	while b > 0 and c < 0:
		if seq[i] == '1':
			seq[i] = '2'
			b -= 1
			c += 1
		i -= 1
if b < 0:
	i = len(seq) - 1
	while a > 0 and b < 0:
		if seq[i] == '0':
			seq[i] = '1'
			a -= 1
			b += 1
		i -= 1
print(''.join(seq))


n = int(input())
word = input()
(x, y, z) = (0, 0, 0)
for i in word:
	if i == '0':
		x += 1
	elif i == '1':
		y += 1
	else:
		z += 1
if x == n // 3 and y == n // 3 and (z == n // 3):
	print(word)
else:
	f = 0
	for i in [x, y, z]:
		if i > n // 3:
			f += 1
	if f == 1:
		if x > n // 3:
			ans = list(map(str, word))
			b = n // 3 - y
			c = n // 3 - z
			ind = []
			for i in range(n):
				if ans[i] == '0':
					ind.append(i)
			for i in range(n // 3, len(ind)):
				if b:
					ans[ind[i]] = '1'
					b -= 1
				else:
					ans[ind[i]] = '2'
					c -= 1
			ans = ''.join((str(x) for x in ans))
			print(ans)
		elif y > n // 3:
			ans = list(map(str, word))
			a = n // 3 - x
			c = n // 3 - z
			ind = []
			for i in range(len(word)):
				if ans[i] == '1':
					ind.append(i)
			for i in range(a):
				ans[ind[i]] = '0'
			for i in range(a + n // 3, len(ind)):
				ans[ind[i]] = '2'
			ans = ''.join((str(x) for x in ans))
			print(ans)
		else:
			ans = list(map(str, word))
			a = n // 3 - x
			b = n // 3 - y
			ind = []
			for i in range(len(word)):
				if ans[i] == '2':
					ind.append(i)
			for i in range(a):
				ans[ind[i]] = '0'
			for i in range(a, a + b):
				ans[ind[i]] = '1'
			ans = ''.join((str(x) for x in ans))
			print(ans)
	elif x > n // 3 and y > n // 3:
		ans = list(map(str, word))
		(ind1, ind2) = ([], [])
		for i in range(n):
			if word[i] == '0':
				ind1.append(i)
			elif word[i] == '1':
				ind2.append(i)
		ind1 = ind1[n // 3:]
		ind2 = ind2[n // 3:]
		for i in ind1:
			ans[i] = '2'
		for i in ind2:
			ans[i] = '2'
		ans = ''.join((str(x) for x in ans))
		print(ans)
	elif y > n // 3 and z > n // 3:
		ans = list(map(str, word))
		(ind1, ind2) = ([], [])
		for i in range(len(word)):
			if word[i] == '1':
				ind1.append(i)
			elif word[i] == '2':
				ind2.append(i)
		for i in range(y - n // 3):
			ans[ind1[i]] = '0'
		for j in range(z - n // 3):
			ans[ind2[j]] = '0'
		ans = ''.join((str(x) for x in ans))
		print(ans)
	else:
		ans = list(map(str, word))
		(ind1, ind2) = ([], [])
		for i in range(len(word)):
			if word[i] == '0':
				ind1.append(i)
			elif word[i] == '2':
				ind2.append(i)
		for i in range(n // 3, len(ind1)):
			ans[ind1[i]] = '1'
		for j in range(len(ind2) - n // 3):
			ans[ind2[j]] = '1'
		ans = ''.join((str(x) for x in ans))
		print(ans)


n = int(input())
s = [int(z) for z in input().strip()]
c = [0] * 3
for i in range(3):
	c[i] = s.count(i)
td = [0] * 3
for i in range(3):
	td[i] = n // 3 - c[i]
for (f, t) in ((0, 2), (2, 0), (0, 1), (1, 0), (1, 2), (2, 1)):
	if td[f] < 0 and td[t] > 0:
		ni = min(abs(td[f]), abs(td[t]))
		if f > t:
			for i in range(n):
				if s[i] == f:
					s[i] = t
					ni -= 1
				if ni == 0:
					break
		else:
			for i in range(n - 1, -1, -1):
				if s[i] == f:
					s[i] = t
					ni -= 1
				if ni == 0:
					break
print(*s, sep='')


n = int(input())
s = list(input())
d = {i: 0 for i in '012'}
for i in s:
	d[i] += 1
eq = n // 3
i = 0
while d['0'] < eq:
	if s[i] != '0':
		if d[s[i]] > eq:
			d[s[i]] -= 1
			d['0'] += 1
			s[i] = '0'
	i += 1
i = n - 1
while d['2'] < eq:
	if s[i] != '2':
		if d[s[i]] > eq:
			d[s[i]] -= 1
			d['2'] += 1
			s[i] = '2'
	i -= 1
i = n - 1
while d['1'] < eq and i >= 0:
	if s[i] == '0':
		if d[s[i]] > eq:
			d[s[i]] -= 1
			d['1'] += 1
			s[i] = '1'
	i -= 1
i = 0
while d['1'] < eq and i < n:
	if s[i] == '2':
		if d[s[i]] > eq:
			d[s[i]] -= 1
			d['1'] += 1
			s[i] = '1'
	i += 1
print(*s, sep='')


n = int(input())
l = list(input())
cnt = dict()
cnt['0'] = l.count('0')
cnt['1'] = l.count('1')
cnt['2'] = l.count('2')
for i in range(n):
	if cnt['0'] < n / 3:
		if cnt[l[i]] > n / 3:
			cnt[l[i]] -= 1
			cnt['0'] += 1
			l[i] = '0'
for i in reversed(range(n)):
	if cnt['2'] < n / 3:
		if cnt[l[i]] > n / 3:
			cnt[l[i]] -= 1
			cnt['2'] += 1
			l[i] = '2'
for i in range(len(l)):
	if cnt['1'] < n / 3:
		if cnt['2'] > n / 3:
			if l[i] == '2':
				cnt['2'] -= 1
				cnt['1'] += 1
				l[i] = '1'
for i in reversed(range(n)):
	if cnt['1'] < n / 3:
		if cnt['0'] > n / 3:
			if l[i] == '0':
				cnt['0'] -= 1
				cnt['1'] += 1
				l[i] = '1'
result = ''
for i in l:
	result += i
print(result)


from collections import Counter
n = int(input())
s = input().strip()
x = n // 3
cnt = Counter(s)
cnt.update({'0': 0, '1': 0, '2': 0})
if cnt['0'] == cnt['1'] == cnt['2']:
	print(s)
	exit()
while max(cnt.values()) != min(cnt.values()):
	p = cnt['0']
	q = cnt['1']
	r = cnt['2']
	if r > x:
		if p < x:
			if r - x >= x - p:
				s = s.replace('2', '0', x - p)
				r = r - (x - p)
				cnt['2'] = r
				p = x
				cnt['0'] = p
			else:
				s = s.replace('2', '0', r - x)
				p = p + r - x
				cnt['0'] = p
				r = x
				cnt['2'] = r
		if q < x:
			if r - x >= x - q:
				s = s.replace('2', '1', x - q)
				r = r - (x - q)
				cnt['2'] = r
				q = x
				cnt['1'] = q
			else:
				s = s.replace('2', '1', r - x)
				q = q + r - x
				cnt['1'] = q
				r = x
				cnt['2'] = r
	if q > x:
		if p < x:
			if q - x >= x - p:
				s = s.replace('1', '0', x - p)
				q = q - (x - p)
				cnt['1'] = q
				p = x
				cnt['0'] = p
			else:
				s = s.replace('1', '0', q - x)
				p = p + q - x
				cnt['0'] = p
				q = x
				cnt['1'] = q
		if r < x:
			if q - x >= x - r:
				s = s[::-1].replace('1', '2', x - r)[::-1]
				q = q - (x - r)
				cnt['1'] = q
				r = x
				cnt['2'] = r
			else:
				s = s[::-1].replace('1', '2', q - x)[::-1]
				r = r + q - x
				cnt['2'] = r
				q = x
				cnt['1'] = q
	if p > x:
		if r < x:
			if p - x >= x - r:
				s = s[::-1].replace('0', '2', x - r)[::-1]
				p = p - (x - r)
				cnt['0'] = p
				r = x
				cnt['2'] = r
			else:
				s = s[::-1].replace('0', '2', p - x)[::-1]
				r = r + p - x
				cnt['2'] = r
				p = x
				cnt['0'] = p
		if q < x:
			if p - x >= x - q:
				s = s[::-1].replace('0', '1', x - q)[::-1]
				p = p - (x - q)
				cnt['0'] = p
				q = x
				cnt['1'] = q
			else:
				s = s[::-1].replace('0', '1', r - x)[::-1]
				q = q + r - x
				cnt['1'] = q
				r = x
				cnt['0'] = r
print(s)


n = int(input())
goal = n // 3
s = input()
(a, b, c) = (s.count('0'), s.count('1'), s.count('2'))
s = list(s)
for i in range(len(s)):
	if c <= goal:
		break
	if s[i] == '2' and c > goal:
		if a < goal:
			s[i] = '0'
			a += 1
		elif b < goal:
			s[i] = '1'
			b += 1
		c -= 1
for i in range(len(s) - 1, -1, -1):
	if a <= goal:
		break
	if s[i] == '0' and a > goal:
		if c < goal:
			s[i] = '2'
			c += 1
		elif b < goal:
			s[i] = '1'
			b += 1
		a -= 1
for i in range(len(s)):
	if b <= goal:
		break
	if s[i] == '1':
		if a < goal:
			s[i] = '0'
			b -= 1
			a += 1
for i in range(len(s) - 1, -1, -1):
	if b <= goal:
		break
	if s[i] == '1':
		if c < goal:
			s[i] = '2'
			b -= 1
			c += 1
print(''.join(s))


n = int(input())
a = list(map(int, input()))
n0 = len([x for x in a if x == 0])
n1 = len([x for x in a if x == 1])
n2 = n - n0 - n1
(n0, n1, n2) = (n0 - n / 3, n1 - n / 3, n2 - n / 3)
if n0 > 0 and n2 < 0:
	for i in range(len(a) - 1, -1, -1):
		if a[i] == 0:
			a[i] = 2
			n0 -= 1
			n2 += 1
		if n0 == 0 or n2 == 0:
			break
if n0 < 0 and n2 > 0:
	for i in range(len(a)):
		if a[i] == 2:
			a[i] = 0
			n0 += 1
			n2 -= 1
		if n0 == 0 or n2 == 0:
			break
if n1 > 0 and n2 < 0:
	for i in range(len(a) - 1, -1, -1):
		if a[i] == 1:
			a[i] = 2
			n1 -= 1
			n2 += 1
		if n1 == 0 or n2 == 0:
			break
if n1 < 0 and n2 > 0:
	for i in range(len(a)):
		if a[i] == 2:
			a[i] = 1
			n1 += 1
			n2 -= 1
		if n1 == 0 or n2 == 0:
			break
if n0 < 0 and n1 > 0:
	for i in range(len(a)):
		if a[i] == 1:
			a[i] = 0
			n0 += 1
			n1 -= 1
		if n0 == 0 or n1 == 0:
			break
if n0 > 0 and n1 < 0:
	for i in range(len(a) - 1, -1, -1):
		if a[i] == 0:
			a[i] = 1
			n0 -= 1
			n1 += 1
		if n0 == 0 or n1 == 0:
			break
print(''.join([str(l) for l in a]))


n = int(input())
s = list(input())
have = {'0': s.count('0'), '1': s.count('1'), '2': s.count('2')}
accum = {'0': 0, '1': 0, '2': 0}
need = n // 3
for (i, val) in enumerate(s):
	accum[val] += 1
	if val == '0' and accum[val] > need:
		if have['1'] < need:
			s[i] = '1'
			have['1'] += 1
			have[val] -= 1
			accum[val] -= 1
		elif have['2'] < need:
			s[i] = '2'
			have['2'] += 1
			have[val] -= 1
			accum[val] -= 1
	elif val == '1':
		if have[val] > need and have['0'] < need:
			s[i] = '0'
			have['0'] += 1
			have[val] -= 1
			accum[val] -= 1
		elif accum[val] > need:
			s[i] = '2'
			have['2'] += 1
			have[val] -= 1
			accum[val] -= 1
	elif val == '2' and have[val] > need:
		if have['0'] < need:
			s[i] = '0'
			have['0'] += 1
			have[val] -= 1
			accum[val] -= 1
		elif have['1'] < need:
			s[i] = '1'
			have['1'] += 1
			have[val] -= 1
			accum[val] -= 1
print(''.join(s))


(n, s) = (int(input()), [])
s1 = input()
for i in s1:
	s.append(int(i))
(one, two, cn, k) = (s.count(0), s.count(1), n // 3, 0)
three = n - one - two
d = [[0, one], [1, two], [2, three]]
while not d[0][1] == d[2][1] == cn:
	d.sort(key=lambda x: x[1])
	if s.count(0) < cn:
		d.sort(key=lambda x: x[0] if x[1] < cn else x[1])
	else:
		d.sort(key=lambda x: -x[0] if x[1] < cn else x[1])
	if d[0][0] > d[2][0]:
		(v, k) = (0, n - 1)
	else:
		(v, k) = (1, 0)
	while not d[0][1] == cn and (not d[2][1] == cn):
		if s[k] == d[2][0]:
			s[k] = d[0][0]
			d[0][1] += 1
			d[2][1] += -1
		k = k + 2 * v - 1
print(''.join((str(i) for i in s)))


n = int(input())
s = input()
a = [0, 0, 0]
ans = []
p = 0
for i in s:
	ans.append(i)
	if i == '1':
		a[1] += 1
	elif i == '0':
		a[0] += 1
	else:
		a[2] += 1
	p += 1
k = n // 3
while a[0] > k or a[1] > k or a[2] > k:
	while a[0] < k:
		for i in range(n):
			if ans[i] == '1' and a[1] > k and (a[0] < k):
				ans[i] = '0'
				a[0] += 1
				a[1] -= 1
			elif ans[i] == '2' and a[2] > k and (a[0] < k):
				ans[i] = '0'
				a[2] -= 1
				a[0] += 1
			if a[0] == k:
				break
	while a[1] < k and a[2] > k:
		for i in range(n):
			if ans[i] == '2' and a[2] > k and (a[1] < k):
				ans[i] = '1'
				a[2] -= 1
				a[1] += 1
			if a[1] == k:
				break
	while a[2] < k:
		for i in range(n - 1, -1, -1):
			if ans[i] == '1' and a[1] > k and (a[2] < k):
				ans[i] = '2'
				a[2] += 1
				a[1] -= 1
			elif ans[i] == '0' and a[0] > k and (a[2] < k):
				ans[i] = '2'
				a[0] -= 1
				a[2] += 1
			if a[2] == k:
				break
	while a[1] < k and a[0] > k:
		for i in range(n - 1, -1, -1):
			if ans[i] == '0' and a[0] > k and (a[1] < k):
				ans[i] = '1'
				a[0] -= 1
				a[1] += 1
			if a[1] == k:
				break
t = ''
for i in ans:
	t = t + i
print(t)


n = int(input())
t = list(input())
f = {0: 0, 1: 0, 2: 0}
for j in t:
	if j == '2':
		f[2] += 1
	elif j == '1':
		f[1] += 1
	else:
		f[0] += 1
a = n // 3
for j in range(n):
	if t[j] == '2':
		if f[2] > a:
			if f[0] < a:
				t[j] = '0'
				f[2] -= 1
				f[0] += 1
			elif f[1] < a:
				t[j] = '1'
				f[1] += 1
				f[2] -= 1
	elif t[j] == '1':
		if f[1] > a:
			if f[0] < a:
				t[j] = '0'
				f[0] += 1
				f[1] -= 1
for j in range(n - 1, -1, -1):
	if t[j] == '0':
		if f[0] > a:
			if f[2] < a:
				t[j] = '2'
				f[2] += 1
				f[0] -= 1
			elif f[1] < a:
				t[j] = '1'
				f[1] += 1
				f[0] -= 1
	elif t[j] == '1':
		if f[1] > a:
			if f[2] < a:
				t[j] = '2'
				f[2] += 1
				f[1] -= 1
print(''.join(t))


n = int(input())
s = [*map(str, input())]
co = ct = cz = 0
f = n // 3
(o, t, z) = (s.count('1'), s.count('2'), s.count('0'))
for i in range(n):
	if s[i] == '0':
		if o < f and z > f and (cz >= f):
			s[i] = '1'
			o += 1
			z -= 1
		elif t < f and z > f and (cz >= f):
			s[i] = '2'
			t += 1
			z -= 1
		else:
			cz += 1
	if s[i] == '1':
		if z < f and o > f:
			s[i] = '0'
			z += 1
			o -= 1
		elif t < f and o > f and (co >= f):
			s[i] = '2'
			t += 1
			o -= 1
		else:
			co += 1
	if s[i] == '2':
		if z < f and t > f:
			s[i] = '0'
			z += 1
			t -= 1
		elif o < f and t > f:
			s[i] = '1'
			o += 1
			t -= 1
		else:
			ct += 1
print(''.join(s))


from collections import Counter
n = int(input())
A = list(input())
desired = n // 3
count = Counter(A)
letters = ['0', '1', '2']
for i in letters:
	for j in letters[::2 * (i != '0') - 1]:
		if i < j:
			for k in range(len(A) - 1, -1, -1):
				if A[k] == i and count[i] > desired and (count[j] < desired):
					A[k] = j
					count[i] -= 1
					count[j] += 1
		else:
			for k in range(len(A)):
				if A[k] == i and count[i] > desired and (count[j] < desired):
					A[k] = j
					count[i] -= 1
					count[j] += 1
assert count['0'] == desired
assert count['1'] == desired
assert count['2'] == desired
print(''.join(A))


import math
import bisect
import itertools
import sys
I = lambda : sys.stdin.readline()
mod = 10 ** 9 + 7

def mindiff(a):
	b = a[:]
	b.sort()
	m = 10000000000
	for i in range(len(b) - 1):
		if b[i + 1] - b[i] < m:
			m = b[i + 1] - b[i]
	return m

def lcm(a, b):
	return a * b // math.gcd(a, b)

def merge(a, b):
	i = 0
	j = 0
	c = 0
	ans = []
	while i < len(a) and j < len(b):
		if a[i] < b[j]:
			ans.append(a[i])
			i += 1
		else:
			ans.append(b[j])
			c += len(a) - i
			j += 1
	ans += a[i:]
	ans += b[j:]
	return (ans, c)

def mergesort(a):
	if len(a) == 1:
		return (a, 0)
	mid = len(a) // 2
	(left, left_inversion) = mergesort(a[:mid])
	(right, right_inversion) = mergesort(a[mid:])
	(m, c) = merge(left, right)
	c += left_inversion + right_inversion
	return (m, c)

def is_prime(num):
	if num == 1:
		return False
	if num == 2:
		return True
	if num == 3:
		return True
	if num % 2 == 0:
		return False
	if num % 3 == 0:
		return False
	t = 5
	a = 2
	while t <= int(math.sqrt(num)):
		if num % t == 0:
			return False
		t += a
		a = 6 - a
	return True

def ceil(a, b):
	if a % b == 0:
		return a // b
	else:
		return a // b + 1

def binsearch(arr, b, low, high):
	if low == high:
		return low
	if arr[math.ceil((low + high) / 2)] < b:
		return binsearch(arr, b, low, math.ceil((low + high) / 2) - 1)
	else:
		return binsearch(arr, b, math.ceil((low + high) / 2), high)

def ncr1(n, r):
	s = 1
	for i in range(min(n - r, r)):
		s *= n - i
		s %= mod
		s *= pow(i + 1, mod - 2, mod)
		s %= mod
	return s

def calc(n, m, r):
	s = 0
	for i in range(0, r + 1, 2):
		s += ncr1(n, i) * ncr1(m, i)
		s %= mod
	return s
n = int(input())
s = input()
c = ''
d = {'0': 0, '1': 0, '2': 0}
ans = n // 3
for i in s:
	d[i] += 1
d1 = {'0': 0, '1': 0, '2': 0}
for i in s:
	if d['0'] < ans:
		k = '0'
	elif d['1'] < ans:
		k = '1'
	elif d['2'] < ans:
		k = '2'
	if d[i] > ans:
		if int(i) < int(k) and d1[i] == ans or int(i) > int(k):
			c += k
			d[i] -= 1
			d[k] += 1
			d1[k] += 1
		else:
			c += i
			d1[i] += 1
	else:
		c += i
		d1[i] += 1
print(c)


from sys import stdin, stdout
mod = 10 ** 9 + 7
t = 1
for _ in range(t):
	n = int(input())
	s = input()
	a = s.count('0')
	b = s.count('1')
	c = s.count('2')
	ans = [i for i in s]
	val = n // 3
	ind = -1
	while c > val:
		ind += 1
		if ans[ind] == '2':
			c -= 1
			if a < val:
				ans[ind] = '0'
				a += 1
			else:
				ans[ind] = '1'
				b += 1
	for i in range(n):
		if ans[i] == '1':
			if b > val:
				if a < val:
					b -= 1
					a += 1
					ans[i] = '0'
	for i in range(n - 1, -1, -1):
		if ans[i] == '1':
			if b > val:
				if c < val:
					b -= 1
					c += 1
					ans[i] = '2'
	for i in range(n - 1, -1, -1):
		if ans[i] == '0':
			if a > val:
				if c < val:
					a -= 1
					c += 1
					ans[i] = '2'
	for i in range(n - 1, -1, -1):
		if ans[i] == '0':
			if a > val:
				if b < val:
					a -= 1
					b += 1
					ans[i] = '1'
	print(''.join(ans))


sa = lambda : input()
sb = lambda : int(input())
sc = lambda : input().split()
sd = lambda : list(map(int, input().split()))
se = lambda : float(input())

def hnbhai():
	n = sb()
	s = sa()
	a = list(s)
	r = n // 3
	(zero, one, two) = (0, 0, 0)
	for i in a:
		if i == '0':
			zero += 1
		elif i == '1':
			one += 1
		else:
			two += 1
	if one == zero == two:
		print(s)
		return
	for i in range(n):
		if zero < r:
			if a[i] == '1' and one > r:
				a[i] = '0'
				one -= 1
				zero += 1
			elif a[i] == '2' and two > r:
				a[i] = '0'
				two -= 1
				zero += 1
		if zero >= r:
			break
	for i in range(n):
		if one < r:
			if a[i] == '2' and two > r:
				a[i] = '1'
				two -= 1
				one += 1
		if one >= r:
			break
	for i in range(n - 1, -1, -1):
		if two < r:
			if a[i] == '1' and one > r:
				a[i] = '2'
				one -= 1
				two += 1
			elif a[i] == '0' and zero > r:
				a[i] = '2'
				zero -= 1
				two += 1
		if two >= r:
			break
	for i in range(n - 1, -1, -1):
		if one < r:
			if a[i] == '0' and zero > r:
				a[i] = '1'
				zero -= 1
				one += 1
		if one >= r:
			break
	print(''.join(a))
hnbhai()


n = int(input())
ss = [i for i in input()]
(zz, oo, tt) = (ss.count('0'), ss.count('1'), ss.count('2'))
eq = n // 3
for i in range(0, len(ss)):
	if zz < eq and ss[i] == '2' and (tt > eq):
		ss[i] = '0'
		zz += 1
		tt -= 1
	elif zz < eq and ss[i] == '1' and (oo > eq):
		ss[i] = '0'
		zz += 1
		oo -= 1
for i in range(0, len(ss)):
	if oo < eq and ss[i] == '2' and (tt > eq):
		ss[i] = '1'
		tt -= 1
		oo += 1
for i in range(len(ss) - 1, -1, -1):
	if tt < eq and ss[i] == '1' and (oo > eq):
		ss[i] = '2'
		tt += 1
		oo -= 1
	elif tt < eq and ss[i] == '0' and (zz > eq):
		ss[i] = '2'
		tt += 1
		zz -= 1
for i in range(len(ss) - 1, -1, -1):
	if oo < eq and ss[i] == '0' and (zz > eq):
		ss[i] = '1'
		zz -= 1
		oo += 1
print(''.join(ss))


n = int(input())
a = [int(i) for i in input()]
c = [a.count(i) for i in [0, 1, 2]]
m = n // 3
for x in range(2):
	for i in range(n):
		if c[x] < m and c[a[i]] > m and (a[i] > x):
			print()
			c[a[i]] -= 1
			c[x] += 1
			a[i] = x
for x in range(2, 0, -1):
	for i in range(n - 1, -1, -1):
		if c[x] < m and c[a[i]] > m and (a[i] < x):
			c[a[i]] -= 1
			c[x] += 1
			a[i] = x
print(''.join([str(i) for i in a]))


n = int(input())
s = input()
s = list(s)
(z, o, t) = (0, 0, 0)
for i in range(0, n):
	if s[i] == '1':
		o += 1
	elif s[i] == '0':
		z += 1
	elif s[i] == '2':
		t += 1
for i in range(0, n):
	if s[i] == '1' and o > n // 3 and (z < n // 3):
		s[i] = '0'
		z += 1
		o -= 1
	elif s[i] == '2':
		if t > n // 3 and z < n // 3:
			s[i] = '0'
			z += 1
			t -= 1
		elif t > n // 3 and o < n // 3:
			s[i] = '1'
			t -= 1
			o += 1
for i in range(1, n + 1):
	if s[-i] == '0':
		if z > n // 3 and t < n // 3:
			s[-i] = '2'
			t += 1
			z -= 1
		elif z > n // 3 and o < n // 3:
			s[-i] = '1'
			o += 1
			z -= 1
	elif s[-i] == '1' and o > n // 3 and (t < n // 3):
		s[-i] = '2'
		o -= 1
		t += 1
a = ''.join((str(a) for a in s))
print(a)


from sys import stdin
from collections import Counter
from itertools import zip_longest

def rplc(text, index=0, replacement=''):
	return '%s%s%s' % (text[:index], replacement, text[index + 1:])
n = stdin.readline()
n = int(n)
s = stdin.readline()
s = s.rstrip('\n')
s = list(s)
per_c = n // 3
cnt = Counter(s)

def replace_left_2_right(old, new):
	global s
	global cnt
	for (i, c) in enumerate(s):
		if cnt[new] >= per_c:
			break
		if c == old and cnt[old] > per_c and (cnt[new] < per_c):
			s[i] = new
			cnt[new] += 1
			cnt[old] -= 1

def replace_right_2_left(old, new):
	global s
	global cnt
	for j in range(n):
		i = n - j - 1
		c = s[i]
		if cnt[new] >= per_c:
			break
		if c == old and cnt[old] > per_c and (cnt[new] < per_c):
			s[i] = new
			cnt[new] += 1
			cnt[old] -= 1
a = '2'
for b in ['0', '1']:
	replace_left_2_right(a, b)
a = '0'
for b in ['2', '1']:
	replace_right_2_left(a, b)
replace_right_2_left('1', '2')
replace_left_2_right('1', '0')
print(''.join(s))


n = int(input())
s = input()
n = n // 3
d = {}
for item in s:
	d[item] = d.get(item, 0) + 1
d2 = [0, 0, 0]
res = []
for item in s:
	if d[item] <= n:
		res.append(item)
		continue
	if d[item] > n:
		ok = True
		for i in ['0', '1', '2']:
			if d.get(i, 0) < n:
				if d2[int(item)] >= n or i < item:
					d[item] -= 1
					d[i] = d.get(i, 0) + 1
					res.append(i)
				else:
					d2[int(item)] += 1
					res.append(item)
				break
print(''.join(map(str, res)))


n = int(input())
s = list(input())
cnt = n // 3
(z, o, t) = (s.count('0'), s.count('1'), s.count('2'))
for i in range(n):
	if z < cnt:
		if s[i] == '2' and t > cnt:
			s[i] = '0'
			z += 1
			t -= 1
		elif s[i] == '1' and o > cnt:
			s[i] = '0'
			z += 1
			o -= 1
for i in range(n - 1, -1, -1):
	if t < cnt:
		if s[i] == '1' and o > cnt:
			s[i] = '2'
			t += 1
			o -= 1
		elif s[i] == '0' and z > cnt:
			s[i] = '2'
			t += 1
			z -= 1
for i in range(n):
	if t > cnt:
		if s[i] == '2':
			s[i] = '1'
			o += 1
			t -= 1
for i in range(n - 1, -1, -1):
	if z > cnt:
		if s[i] == '0':
			s[i] = '1'
			z -= 1
			o += 1
print(''.join(s))


from sys import stdin, stdout
from math import *
from heapq import *
from collections import *

def replace(x, y):
	global n, s, a, inc, dec, c, k
	if x == y:
		return
	order = dec
	if y < x:
		order = inc
	for i in order:
		if (c[x] > k and c[y] < k) == False:
			return
		if a[i] == x:
			c[x] = c[x] - 1
			a[i] = y
			c[y] = c[y] + 1

def main():
	global n, s, a, inc, dec, c, k
	inc = []
	dec = []
	n = int(stdin.readline())
	s = stdin.readline().rstrip('\n')
	a = []
	for x in s:
		a.append(int(x))
	c = [0, 0, 0, 0]
	for i in range(n):
		inc.append(i)
		dec.append(n - i - 1)
		c[a[i]] = c[a[i]] + 1
	k = trunc(n / 3)
	replace(2, 0)
	replace(2, 1)
	replace(1, 0)
	replace(0, 2)
	replace(0, 1)
	replace(1, 2)
	stdout.write(''.join([str(x) for x in a]))
	return 0
main()


n = int(input())
w = int(n / 3)
s = list(input())
x1 = 0
x2 = 0
x3 = 0
for i in s:
	if i == '0':
		x1 += 1
	elif i == '1':
		x2 += 1
	else:
		x3 += 1
if x1 > w and x2 > w:
	o1 = x1 - w
	o2 = x2 - w
	for i in range(n):
		if s[n - i - 1] == '1' and o2 > 0:
			o2 -= 1
			s[n - i - 1] = '2'
		if s[n - i - 1] == '0' and o1 > 0:
			o1 -= 1
			s[n - i - 1] = '2'
elif x3 > w and x2 > w:
	o1 = x3 - w
	o2 = x2 - w
	for i in range(n):
		if s[i] == '1' and o2 > 0:
			o2 -= 1
			s[i] = '0'
		if s[i] == '2' and o1 > 0:
			o1 -= 1
			s[i] = '0'
elif x1 > w and x3 > w:
	o1 = x1 - w
	o2 = x3 - w
	for i in range(n):
		if s[n - i - 1] == '0' and o1 > 0:
			o1 -= 1
			s[n - i - 1] = '1'
	for i in range(n):
		if s[i] == '2' and o2 > 0:
			o2 -= 1
			s[i] = '1'
elif x1 > w:
	o = w - x3
	o1 = w - x2
	for i in range(n):
		if s[n - i - 1] == '0' and o >= 1:
			o -= 1
			s[n - i - 1] = '2'
	for i in range(n):
		if s[n - i - 1] == '0' and o1 >= 1:
			o1 -= 1
			s[n - i - 1] = '1'
elif x2 > w:
	o = w - x3
	o1 = w - x1
	for i in range(n):
		if s[n - i - 1] == '1' and o >= 1:
			o -= 1
			s[n - i - 1] = '2'
	for i in range(n):
		if s[i] == '1' and o1 >= 1:
			o1 -= 1
			s[i] = '0'
elif x3 > w:
	o = w - x1
	o1 = w - x2
	for i in range(n):
		if s[i] == '2' and o >= 1:
			o -= 1
			s[i] = '0'
	for i in range(n):
		if s[i] == '2' and o1 >= 1:
			o1 -= 1
			s[i] = '1'
for i in s:
	print(i, end='')


n = int(input())
m = n // 3
s = list(input())
(zero, one, two) = (0, 0, 0)
for si in s:
	if si == '0':
		zero += 1
	elif si == '1':
		one += 1
	else:
		two += 1
zero_lack = max(0, m - zero)
one_lack = max(0, m - one)
two_lack = max(0, m - two)
if zero > m:
	zero_over = zero - m
	for i in range(n - 1, -1, -1):
		if s[i] == '0':
			if two_lack > 0:
				s[i] = '2'
				two_lack -= 1
				zero_over -= 1
			elif one_lack > 0:
				s[i] = '1'
				one_lack -= 1
				zero_over -= 1
			if zero_over == 0:
				break
if one > m:
	one_over = one - m
	for i in range(n):
		if s[i] == '1':
			if zero_lack > 0:
				s[i] = '0'
				zero_lack -= 1
				one_over -= 1
			if one_over == 0:
				break
	for i in range(n - 1, -1, -1):
		if s[i] == '1':
			if two_lack > 0:
				s[i] = '2'
				two_lack -= 1
				one_over -= 1
			if one_over == 0:
				break
if two > m:
	two_over = two - m
	for i in range(n):
		if s[i] == '2':
			if zero_lack > 0:
				s[i] = '0'
				zero_lack -= 1
				two_over -= 1
			elif one_lack > 0:
				s[i] = '1'
				one_lack -= 1
				two_over -= 1
			if two_over == 0:
				break
print(''.join(s))


def main():
	n = int(input())
	s = input()
	expected = len(s) // 3
	cnt = {'0': s.count('0'), '1': s.count('1'), '2': s.count('2')}
	s = list(s)
	for i in range(len(s)):
		if cnt[s[i]] > expected:
			if s[i] == '1' or s[i] == '2':
				source = s[i]
				if cnt['0'] < expected:
					s[i] = '0'
					cnt['0'] += 1
					cnt[source] -= 1
				elif source == '2' and cnt['1'] < expected:
					s[i] = '1'
					cnt['1'] += 1
					cnt['2'] -= 1
	for i in range(len(s) - 1, -1, -1):
		if cnt[s[i]] > expected:
			source = s[i]
			target = '2' if cnt['2'] < expected else '1'
			s[i] = target
			cnt[source] -= 1
			cnt[target] += 1
	print(''.join(map(str, s)))
main()


n = int(input())
ar = input()
ll = [int(i) for i in str(ar)]
n0 = ll.count(0)
n1 = ll.count(1)
n2 = ll.count(2)
s = (n1 + n2 + n0) / 3
t = list()
cn0 = 0
cn1 = 0
for i in range(n):
	if ll[i] == 0:
		cn0 += 1
		if cn0 > s:
			if n1 < s:
				t.append(1)
				n1 = n1 + 1
			elif n2 < s:
				t.append(2)
				n2 = n2 + 1
			n0 = n0 - 1
			cn0 = cn0 - 1
		else:
			t.append(0)
	if ll[i] == 1:
		cn1 += 1
		if n1 > s:
			if n0 < s:
				t.append(0)
				n0 = n0 + 1
			elif cn1 <= s:
				t.append(1)
				n1 += 1
				cn1 += 1
			elif n2 < s:
				t.append(2)
				n2 = n2 + 1
			n1 = n1 - 1
			cn1 = cn1 - 1
		else:
			t.append(1)
	if ll[i] == 2:
		if n2 > s:
			if n0 < s:
				t.append(0)
				n0 = n0 + 1
			elif n1 < s:
				t.append(1)
				n1 = n1 + 1
			n2 = n2 - 1
		else:
			t.append(2)
for i in t:
	print(i, end='')


n = int(input())
s = [*input()]
(a, b, c) = (0, 0, 0)
for i in range(n):
	if s[i] == '0':
		a += 1
	if s[i] == '1':
		b += 1
	if s[i] == '2':
		c += 1
(na, nb, nc) = (n // 3 - a, n // 3 - b, n // 3 - c)
for i in range(n):
	if s[i] == '1' and na > 0 and (nb < 0):
		s[i] = '0'
		na -= 1
		nb += 1
	if s[i] == '2' and nc < 0:
		if na > 0:
			s[i] = '0'
			na -= 1
			nc += 1
		elif nb > 0:
			s[i] = '1'
			nb -= 1
			nc += 1
for i in range(n - 1, -1, -1):
	if s[i] == '1' and nb < 0 and (nc > 0):
		s[i] = '2'
		nb += 1
		nc -= 1
	if s[i] == '0' and na < 0:
		if nc > 0:
			s[i] = '2'
			na += 1
			nc -= 1
		elif nb > 0:
			s[i] = '1'
			na += 1
			nb -= 1
print(''.join(s))


from collections import Counter
gi = lambda : list(map(int, input().strip().split()))
(n,) = gi()
s = list(map(int, list(input())))
occ = Counter(s)
for e in [0, 1, 2]:
	if e not in occ:
		occ[e] = 0
ans = ['0'] * n
if occ[0] >= n // 3:
	x = 0
	for k in range(n):
		if s[k] == 0:
			s[k] = 9
			x += 1
		if x == n // 3:
			break
else:
	for k in range(n):
		if s[k] == 0:
			s[k] = 9
		elif occ[s[k]] > n // 3 and occ[0] < n // 3:
			occ[s[k]] -= 1
			occ[0] += 1
			s[k] = 9
			ans[k] = '0'
if occ[1] >= n // 3:
	x = 0
	for k in range(n):
		if s[k] == 1:
			s[k] = 9
			x += 1
			ans[k] = '1'
		if x == n // 3:
			break
else:
	for k in range(n):
		if s[k] == 1:
			s[k] = 9
			ans[k] = '1'
		elif s[k] != 9 and occ[s[k]] > n // 3 and (occ[1] < n // 3):
			occ[s[k]] -= 1
			s[k] = 9
			ans[k] = '1'
			occ[1] += 1
for k in range(n):
	if s[k] != 9:
		ans[k] = '2'
print(''.join(ans))


from sys import stdin

def update(a, idx1, idx2):
	a[idx1] += 1
	a[idx2] -= 1
	return a

def check(a):
	if '0' not in a:
		a['0'] = 0
	if '1' not in a:
		a['1'] = 0
	if '2' not in a:
		a['2'] = 0
	return a
n = int(stdin.readline())
s = stdin.readline()
arr = {}
(z, o, t) = (0, 0, 0)
for i in range(n):
	if s[i] == '0':
		z += 1
	elif s[i] == '1':
		o += 1
	else:
		t += 1
count = n // 3
ans = list(s)
(zc, oc, tc) = (0, 0, 0)
for i in range(n):
	if s[i] == '0':
		zc += 1
	elif s[i] == '1':
		oc += 1
	else:
		tc += 1
	if z < count:
		if s[i] == '1' and o > count:
			ans[i] = '0'
			(z, zc) = (z + 1, zc + 1)
			(o, oc) = (o - 1, oc - 1)
		elif s[i] == '2' and t > count:
			ans[i] = '0'
			(z, zc) = (z + 1, zc + 1)
			(t, tc) = (t - 1, tc - 1)
	elif o < count:
		if s[i] == '0' and z > count and (zc > count):
			ans[i] = '1'
			(z, zc) = (z - 1, zc - 1)
			(o, oc) = (o + 1, oc + 1)
		elif s[i] == '2' and t > count:
			ans[i] = '1'
			(o, oc) = (o + 1, oc + 1)
			(t, tc) = (t - 1, tc - 1)
	elif t < count:
		if s[i] == '0' and z > count and (zc > count):
			ans[i] = '2'
			(z, zc) = (z - 1, zc - 1)
			(t, tc) = (t + 1, tc + 1)
		elif s[i] == '1' and o > count and (oc > count):
			ans[i] = '2'
			(o, oc) = (o - 1, oc - 1)
			(t, tc) = (t + 1, tc + 1)
	if t == o == z:
		break
print(''.join(ans))


def replace_char_from_start(n, result, nb_to_replace_counter, char, char1, char2=None):
	index = 0
	while index < n and nb_to_replace_counter[char] > 0:
		if result[index] == char:
			if nb_to_replace_counter[char1] < 0:
				result[index] = char1
				nb_to_replace_counter[char1] += 1
				nb_to_replace_counter[char] -= 1
			elif char2 is not None:
				result[index] = char2
				nb_to_replace_counter[char2] += 1
				nb_to_replace_counter[char] -= 1
			else:
				break
		index += 1
	return result

def replace_char_from_end(n, result, nb_to_replace_counter, char, char1, char2=None):
	index = n - 1
	while index >= 0 and nb_to_replace_counter[char] > 0:
		if result[index] == char:
			if nb_to_replace_counter[char1] < 0:
				result[index] = char1
				nb_to_replace_counter[char1] += 1
				nb_to_replace_counter[char] -= 1
			elif char2 is not None:
				result[index] = char2
				nb_to_replace_counter[char2] += 1
				nb_to_replace_counter[char] -= 1
			else:
				break
		index -= 1
	return result

def solve(n, s):
	counter = {'0': 0, '1': 0, '2': 0}
	result = []
	for index in range(n):
		char = s[index]
		counter[char] += 1
		result.append(char)
	target_char_count = n // 3
	if counter['0'] == target_char_count and counter['0'] == counter['1']:
		return s
	nb_to_replace_counter = {'0': 0, '1': 0, '2': 0}
	nb_to_replace_counter['2'] = counter['2'] - target_char_count
	nb_to_replace_counter['1'] = counter['1'] - target_char_count
	nb_to_replace_counter['0'] = counter['0'] - target_char_count
	result = replace_char_from_start(n, result, nb_to_replace_counter, '2', '0', '1')
	result = replace_char_from_end(n, result, nb_to_replace_counter, '0', '2', '1')
	if nb_to_replace_counter['1'] > 0 and nb_to_replace_counter['0'] < 0:
		result = replace_char_from_start(n, result, nb_to_replace_counter, '1', '0')
	if nb_to_replace_counter['1'] > 0 and nb_to_replace_counter['2'] < 0:
		result = replace_char_from_end(n, result, nb_to_replace_counter, '1', '2')
	return ''.join(result)
n = int(input().strip())
s = input().strip()
print(solve(n, s))


from collections import deque
n = int(input())
arr = [int(i) for i in input()]
ans = [0 for i in range(n)]
indx = [[] for i in range(3)]
for i in range(n):
	indx[arr[i]].append(i)
for i in range(3):
	indx[i] = deque(indx[i])
x = n // 3
if len(indx[2]) > x:
	diff = len(indx[2]) - x
	while diff > 0:
		if len(indx[0]) < x:
			indx[0].append(indx[2][0])
			indx[2].popleft()
		elif len(indx[1]) < x:
			indx[0].append(indx[2][0])
			indx[2].popleft()
		diff -= 1
if len(indx[1]) > x:
	diff = len(indx[1]) - x
	while diff != 0:
		if len(indx[0]) < x:
			indx[0].append(indx[1][0])
			indx[1].popleft()
		elif len(indx[2]) < x:
			indx[2].append(indx[1][-1])
			indx[1].pop()
		diff -= 1
if len(indx[0]) > x:
	diff = len(indx[0]) - x
	while diff != 0:
		if len(indx[2]) < x:
			indx[2].append(indx[0][-1])
			indx[0].pop()
		elif len(indx[1]) < x:
			indx[1].append(indx[0][-1])
			indx[0].pop()
		diff -= 1
for i in range(3):
	for j in indx[i]:
		ans[j] = i
print(''.join((str(i) for i in ans)))


n = int(input())
s = [int(i) for i in input()]
newn = n // 3

def important(s, newn):
	T = [0, 0, 0]
	for i in s:
		T[i] += 1
	(small, large) = ([], [])
	for i in range(0, 3):
		if T[i] > newn:
			large.append(i)
		elif T[i] < newn:
			small.append(i)
	return (T, small, large)
(T, small, large) = important(s, newn)
while len(small) + len(large) > 0:
	temp = []
	if min(small) < max(large):
		x = min(small)
		J = []
		for i in large:
			if i > x:
				J.append(i)
		c = 0
		for i in s:
			if c < newn - T[x]:
				if i in J:
					if T[i] > newn:
						temp.append(x)
						T[i] -= 1
						c += 1
					else:
						temp.append(i)
				else:
					temp.append(i)
			else:
				temp.append(i)
	else:
		x = max(small)
		c = 0
		for i in s[::-1]:
			if c < newn - T[x]:
				if i in large:
					if T[i] > newn:
						temp.append(x)
						T[i] -= 1
						c += 1
					else:
						temp.append(i)
				else:
					temp.append(i)
			else:
				temp.append(i)
		temp.reverse()
	s = temp
	(T, small, large) = important(s, newn)
for i in s:
	print(i, end='')


n = int(input())
a = list(input())
a = list(map(int, a))
c = [a.count(i) for i in range(3)]

def front(x):
	for i in range(n):
		if c[x] < n // 3 and a[i] > x and (c[a[i]] > n // 3):
			c[x] += 1
			c[a[i]] -= 1
			a[i] = x

def back(x):
	for i in range(n - 1, -1, -1):
		if c[x] < n // 3 and a[i] < x and (c[a[i]] > n // 3):
			c[x] += 1
			c[a[i]] -= 1
			a[i] = x
front(0)
front(1)
back(2)
back(1)
print(''.join([str(i) for i in a]))


from collections import Counter
n = int(input())
s = list(input().strip())
ctr = Counter(s)
target = n // 3
movement = [target - ctr['0'], target - ctr['1'], target - ctr['2']]
if movement[0] > 0:
	if movement[2] < 0:
		tomove = min(movement[0], -movement[2])
		for i in range(n):
			if s[i] == '2':
				s[i] = '0'
				tomove -= 1
				if tomove == 0:
					break
		movement[0] -= tomove
		movement[2] += tomove
	if movement[1] < 0:
		tomove = min(movement[0], -movement[1])
		for i in range(n):
			if s[i] == '1':
				s[i] = '0'
				tomove -= 1
				if tomove == 0:
					break
		movement[0] -= tomove
		movement[1] += tomove
if movement[2] > 0:
	if movement[0] < 0:
		tomove = min(movement[2], -movement[0])
		for i in reversed(range(n)):
			if s[i] == '0':
				s[i] = '2'
				tomove -= 1
				if tomove == 0:
					break
		movement[2] -= tomove
		movement[0] += tomove
	if movement[1] < 0:
		tomove = min(movement[2], -movement[1])
		for i in reversed(range(n)):
			if s[i] == '1':
				s[i] = '2'
				tomove -= 1
				if tomove == 0:
					break
		movement[2] -= tomove
		movement[1] += tomove
if movement[1] > 0:
	if movement[0] < 0:
		tomove = min(movement[1], -movement[0])
		for i in reversed(range(n)):
			if s[i] == '0':
				s[i] = '1'
				tomove -= 1
				if tomove == 0:
					break
		movement[1] -= tomove
		movement[0] += tomove
	if movement[2] < 0:
		tomove = min(movement[1], -movement[2])
		for i in range(n):
			if s[i] == '2':
				s[i] = '1'
				tomove -= 1
				if tomove == 0:
					break
		movement[1] -= tomove
		movement[2] += tomove
print(''.join(s))


n = int(input())
s = list(input())
c = {c: s.count(c) for c in '012'}
i = 0
while c['0'] < n // 3 and i < n:
	if s[i] != '0' and c[s[i]] > n // 3:
		c[s[i]] -= 1
		c['0'] += 1
		s[i] = '0'
	i += 1
i = 0
while c['1'] < n // 3 and i < n:
	if s[i] == '2' and c[s[i]] > n // 3:
		c[s[i]] -= 1
		c['1'] += 1
		s[i] = '1'
	i += 1
i = n - 1
while c['2'] < n // 3 and i > 0:
	if s[i] != '2' and c[s[i]] > n // 3:
		c[s[i]] -= 1
		c['2'] += 1
		s[i] = '2'
	i -= 1
i = n - 1
while c['1'] < n // 3 and i > 0:
	if s[i] != '1' and c[s[i]] > n // 3:
		c[s[i]] -= 1
		c['1'] += 1
		s[i] = '1'
	i -= 1
print(''.join(s))


n = int(input())
a = list(input())
x = a.count('0')
y = a.count('1')
z = a.count('2')
if x < n // 3:
	for i in range(n):
		if a[i] == '1' and y > n // 3:
			a[i] = '0'
			y -= 1
			x += 1
		elif a[i] == '2' and z > n // 3:
			a[i] = '0'
			z -= 1
			x += 1
		if x == n // 3:
			break
if z < n // 3:
	for i in range(n - 1, -1, -1):
		if z == n // 3:
			break
		if a[i] == '1' and y > n // 3:
			a[i] = '2'
			y -= 1
			z += 1
		elif a[i] == '0' and x > n // 3:
			a[i] = '2'
			z += 1
			x -= 1
if y < n // 3:
	for i in range(n):
		if a[i] == '2' and z > n // 3:
			a[i] = '1'
			z -= 1
			y += 1
	for i in range(n - 1, -1, -1):
		if a[i] == '0' and x > n // 3:
			a[i] = '1'
			y += 1
			x -= 1
print(''.join(a))


n = int(input())
s = [i for i in input()]
n3 = n // 3
(a, b, c) = (0, 0, 0)
for i in s:
	if i == '0':
		a += 1
	elif i == '1':
		b += 1
	else:
		c += 1
a -= n3
b -= n3
c -= n3
i = 0
while a < 0 and i < n:
	j = s[i]
	if j == '1' and b > 0 or (j == '2' and c > 0):
		a += 1
		if j == '1':
			b -= 1
		else:
			c -= 1
		s[i] = '0'
	i += 1
i = n - 1
while c < 0 and i >= 0:
	j = s[i]
	if j == '0' and a > 0 or (j == '1' and b > 0):
		c += 1
		if j == '0':
			a -= 1
		else:
			b -= 1
		s[i] = '2'
	i -= 1
i = 0
while b < 0 and i < n:
	j = s[i]
	if j == '2' and c > 0:
		b += 1
		c -= 1
		s[i] = '1'
	i += 1
i = n - 1
while b < 0 and i >= 0:
	j = s[i]
	if j == '0' and a > 0:
		b += 1
		a -= 1
		s[i] = '1'
	i -= 1
print(''.join(s))


n = int(input())
s = input()
num1 = s.count('0')
num2 = s.count('1')
num3 = n - num1 - num2
nums = [num1, num2, num3]
n2 = n // 3
s2 = list(s)
for i in range(n):
	if nums[int(s[i])] > n2:
		temp1 = (int(s[i]) + 1) % 3
		temp2 = (int(s[i]) + 2) % 3
		temp3 = min(temp1, temp2)
		temp4 = max(temp1, temp2)
		if nums[temp3] < n2 and temp3 < int(s[i]):
			nums[temp3] += 1
			nums[int(s[i])] -= 1
			s2[i] = temp3
		elif nums[temp4] < n2 and temp4 < int(s[i]):
			nums[temp4] += 1
			nums[int(s[i])] -= 1
			s2[i] = temp4
for i in range(n - 1, -1, -1):
	if nums[int(s2[i])] > n2:
		temp1 = (int(s2[i]) + 1) % 3
		temp2 = (int(s2[i]) + 2) % 3
		temp3 = max(temp1, temp2)
		temp4 = min(temp1, temp2)
		if nums[temp3] < n2:
			nums[temp3] += 1
			nums[int(s2[i])] -= 1
			s2[i] = temp3
		elif nums[temp4] < n2:
			nums[temp4] += 1
			nums[int(s2[i])] -= 1
			s2[i] = temp4
for i in range(n):
	print(s2[i], end='')


a = int(input())
c = input()
b = [c.count(i) - a // 3 for i in '012']
for i in [0, 2, 1]:
	if b[i] < 0:
		for j in range(3):
			if b[j] > 0:
				m = min(-b[i], b[j])
				b[i] += m
				b[j] -= m
				if i > j:
					c = c[::-1].replace(str(j), str(i), m)[::-1]
				else:
					c = c.replace(str(j), str(i), m)
				if not b[i]:
					break
print(c)


n = int(input())
s = input()
l = list(s)
z = 0
o = 0
t = 0
for i in s:
	if i == '0':
		z += 1
	if i == '1':
		o += 1
	if i == '2':
		t += 1
needed = n // 3
if z < needed:
	k = 0
	while z < needed and k < n and (t > needed):
		if l[k] == '2':
			t = t - 1
			z = z + 1
			l[k] = '0'
		k += 1
	k = 0
	while z < needed and k < n and (o > needed):
		if l[k] == '1':
			o = o - 1
			z = z + 1
			l[k] = '0'
		k += 1
if t < needed:
	k = n - 1
	while t < needed and k > -1 and (o > needed):
		if l[k] == '1':
			o = o - 1
			t = t + 1
			l[k] = '2'
		k -= 1
	k = n - 1
	while t < needed and k > -1 and (z > needed):
		if l[k] == '0':
			z = z - 1
			t = t + 1
			l[k] = '2'
		k -= 1
if o < needed:
	k = 0
	while o < needed and k < n and (t > needed):
		if l[k] == '2':
			t = t - 1
			o = o + 1
			l[k] = '1'
		k += 1
	k = n - 1
	while o < needed and k > -1 and (z > needed):
		if l[k] == '0':
			z = z - 1
			o = o + 1
			l[k] = '1'
		k -= 1
print(''.join(l))


n = int(input())
s = list(input())
us = n // 3
count0 = s.count('0')
count1 = s.count('1')
count2 = s.count('2')
while count0 != count1 or count1 != count2:
	if count0 < us:
		for i in range(n):
			if s[i] != '0':
				if s[i] == '1' and count1 > us:
					count1 -= 1
					count0 += 1
					s[i] = '0'
				elif s[i] == '2' and count2 > us:
					count2 -= 1
					count0 += 1
					s[i] = '0'
			if count0 == us:
				break
	elif count2 < us:
		for i in range(n - 1, -1, -1):
			if s[i] == '1' and count1 > us:
				s[i] = '2'
				count1 -= 1
				count2 += 1
			if count2 == us:
				break
		for i in range(n - 1, -1, -1):
			if s[i] == '0' and count0 > us:
				s[i] = '2'
				count0 -= 1
				count2 += 1
			if count2 == us:
				break
	elif count1 < us:
		for i in range(n):
			if s[i] == '2' and count2 > us:
				s[i] = '1'
				count2 -= 1
				count1 += 1
			if count1 == us:
				break
		for i in range(n - 1, -1, -1):
			if s[i] == '0' and count0 > us:
				s[i] = '1'
				count0 -= 1
				count1 += 1
			if count1 == us:
				break
print(''.join(s))


n = int(input())
s = input()
mas = list(s)
a = [0, 0, 0]
for i in s:
	if i == '0':
		a[0] += 1
	if i == '1':
		a[1] += 1
	if i == '2':
		a[2] += 1
t = int(n / 3)
if a[0] > t and a[1] > t:
	k = n - 1
	for i in range(a[0] - t):
		for j in range(k, -1, -1):
			if mas[j] == '0':
				mas[j] = '2'
				k = j
				break
	k = n - 1
	for i in range(a[1] - t):
		for j in range(k, -1, -1):
			if mas[j] == '1':
				mas[j] = '2'
				k = j
				break
if a[0] > t and a[2] > t:
	k = n - 1
	for i in range(a[0] - t):
		for j in range(k, -1, -1):
			if mas[j] == '0':
				mas[j] = '1'
				k = j
				break
	k = 0
	for i in range(a[2] - t):
		for j in range(k, n):
			if mas[j] == '2':
				mas[j] = '1'
				k = j
				break
if a[1] > t and a[2] > t:
	k = 0
	for i in range(a[1] - t):
		for j in range(k, n):
			if mas[j] == '1':
				mas[j] = '0'
				k = j
				break
	k = 0
	for i in range(a[2] - t):
		for j in range(k, n):
			if mas[j] == '2':
				mas[j] = '0'
				k = j
				break
if a[0] > t and a[1] <= t and (a[2] <= t):
	k = n - 1
	for i in range(t - a[2]):
		for j in range(k, -1, -1):
			if mas[j] == '0':
				mas[j] = '2'
				k = j
				break
	k = n - 1
	for i in range(t - a[1]):
		for j in range(k, -1, -1):
			if mas[j] == '0':
				mas[j] = '1'
				k = j
				break
if a[1] > t and a[0] <= t and (a[2] <= t):
	k = 0
	for i in range(t - a[0]):
		for j in range(k, n):
			if mas[j] == '1':
				mas[j] = '0'
				k = j
				break
	k = n - 1
	for i in range(t - a[2]):
		for j in range(k, -1, -1):
			if mas[j] == '1':
				mas[j] = '2'
				k = j
				break
if a[2] > t and a[0] <= t and (a[1] <= t):
	k = 0
	for i in range(t - a[0]):
		for j in range(k, n):
			if mas[j] == '2':
				mas[j] = '0'
				k = j
				break
	k = 0
	for i in range(t - a[1]):
		for j in range(k, n):
			if mas[j] == '2':
				mas[j] = '1'
				k = j
				break
s = ''.join(mas)
print(s)


def main():
	n = int(input())
	s = str(input())
	need = [n // 3 for i in range(3)]
	d = {}
	d['0'] = 0
	d['1'] = 1
	d['2'] = 2
	ans = []
	for char in s[:]:
		need[d[char]] -= 1
	for i in range(len(s)):
		ans.append(s[i])
	if need[0] > 0:
		for i in range(n):
			if ans[i] != '0' and need[d[ans[i]]] < 0:
				need[d[ans[i]]] += 1
				ans[i] = '0'
				need[0] -= 1
			if need[0] == 0:
				break
	if need[2] > 0:
		for i in range(n - 1, -1, -1):
			if ans[i] != '2' and need[d[ans[i]]] < 0:
				need[d[ans[i]]] += 1
				ans[i] = '2'
				need[2] -= 1
			if need[2] == 0:
				break
	if need[1] > 0:
		for i in range(n - 1, -1, -1):
			if need[1] == 0:
				break
			if ans[i] == '0' and need[0] < 0:
				ans[i] = '1'
				need[0] += 1
				need[1] -= 1
		for i in range(n):
			if need[1] == 0:
				break
			if ans[i] == '2' and need[2] < 0:
				ans[i] = '1'
				need[2] += 1
				need[1] -= 1
	print(''.join((i for i in ans)))
main()


num_dict = {'0': 0, '1': 0, '2': 0}
n = int(input())
limit = int(n / 3)
st = input()
diff = [0 for i in range(3)]
for ch in st:
	num_dict[ch] += 1
for (key, value) in num_dict.items():
	diff[int(key)] = limit - value
st_list = list(st)
for i in range(len(diff)):
	for j in range(len(diff) - 1, i, -1):
		if diff[j] != 0:
			if diff[i] > 0 and diff[j] < 0:
				for m in range(len(st_list)):
					if st_list[m] == str(j):
						st_list[m] = str(i)
						diff[i] -= 1
						diff[j] += 1
					if diff[i] == 0 or diff[j] == 0:
						break
			if diff[i] < 0 and diff[j] > 0:
				for m in range(len(st_list) - 1, -1, -1):
					if st_list[m] == str(i):
						st_list[m] = str(j)
						diff[i] += 1
						diff[j] -= 1
					if diff[i] == 0 or diff[j] == 0:
						break
for x in st_list:
	print(x, end='')


import atexit, io, sys, collections, math, heapq, fractions, copy, os, functools
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def f(ais):
	n = len(ais)
	cc = collections.Counter(ais)
	for k in [0, 1, 2]:
		cc[k] -= n // 3
	if cc[0] < 0:
		for i in range(len(ais)):
			if ais[i] == 0:
				continue
			if cc[ais[i]] > 0:
				cc[ais[i]] -= 1
				cc[0] += 1
				ais[i] = 0
			if cc[0] == 0:
				break
	if cc[1] < 0:
		c = 0
		jj = 99
		for i in range(len(ais)):
			if ais[i] == 1:
				continue
			if ais[i] == 0 and cc[ais[i]] > 0:
				if c >= n // 3:
					cc[1] += 1
					cc[0] -= 1
					ais[i] = 1
			elif ais[i] == 2 and cc[ais[i]] > 0:
				cc[1] += 1
				cc[2] -= 1
				ais[i] = 1
			if ais[i] == 0:
				c += 1
			if cc[1] == 0:
				break
	if cc[2] < 0:
		for i in range(len(ais))[::-1]:
			if ais[i] == 2:
				continue
			if cc[ais[i]] > 0:
				cc[2] += 1
				cc[ais[i]] -= 1
				ais[i] = 2
			if cc[2] == 0:
				break
	return ais
n = int(input())
ais = list(map(int, list(input())))
print(''.join(list(map(str, f(ais)))))


n = int(input())
s = list(input())
a = [0] * 3
for l in s:
	a[int(l)] += 1
ans = [None] * n
i = 0
for l in s:
	if int(l) == 1 and a[0] < n // 3 < a[1]:
		ans[i] = '0'
		a[0] += 1
		a[1] -= 1
	elif int(l) == 2 and a[0] < n // 3 < a[2]:
		ans[i] = '0'
		a[0] += 1
		a[2] -= 1
	elif int(l) == 2 and a[1] < n // 3 < a[2]:
		ans[i] = '1'
		a[1] += 1
		a[2] -= 1
	else:
		ans[i] = l
	i += 1
i = n - 1
for l in ans[::-1]:
	if int(l) == 0 and a[2] < n // 3 < a[0]:
		ans[i] = '2'
		a[2] += 1
		a[0] -= 1
	elif int(l) == 1 and a[2] < n // 3 < a[1]:
		ans[i] = '2'
		a[2] += 1
		a[1] -= 1
	elif int(l) == 0 and a[1] < n // 3 < a[0]:
		ans[i] = '1'
		a[1] += 1
		a[0] -= 1
	i -= 1
print(''.join(ans))


from collections import deque
import pdb
import heapq
from heapq import *
from pprint import pprint

def rI():
	return [int(i) for i in input().split()]
n = rI()[0]
s = input()
k = n // 3
c = {}
c['0'] = 0
c['1'] = 0
c['2'] = 0
for i in s:
	c[i] += 1
s = list(s)
for (ix, i) in enumerate(s):
	if c[i] > k:
		if i == '2':
			if c['0'] < k:
				c[i] -= 1
				c['0'] += 1
				s[ix] = '0'
			else:
				c[i] -= 1
				c['1'] += 1
				s[ix] = '1'
			continue
		if i == '1':
			if c['0'] < k:
				c['0'] += 1
				s[ix] = '0'
				c[i] -= 1
s.reverse()
for (ix, i) in enumerate(s):
	if c[i] > k:
		if i == '0':
			if c['2'] < k:
				c[i] -= 1
				c['2'] += 1
				s[ix] = '2'
			else:
				c[i] -= 1
				c['1'] += 1
				s[ix] = '1'
			continue
		if i == '1':
			if c['2'] < k:
				c['2'] += 1
				s[ix] = '2'
				c[i] -= 1
s.reverse()
print(''.join(s))


import sys
import math
from functools import reduce
import bisect

def getN():
	return int(input())

def getNM():
	return map(int, input().split())

def getList():
	return list(map(int, input().split()))

def input():
	return sys.stdin.readline().rstrip()

def index(a, x):
	i = bisect.bisect_left(a, x)
	if i != len(a) and a[i] == x:
		return i
	return False
n = getN()
arr = list(input())
d = dict()
d['0'] = arr.count('0')
d['1'] = arr.count('1')
d['2'] = arr.count('2')
if d['0'] < n // 3:
	j = 0
	while d['0'] < n // 3:
		if d[arr[j]] > n // 3:
			d[arr[j]] -= 1
			d['0'] += +1
			arr[j] = '0'
		j += 1
if d['2'] < n // 3:
	j = n - 1
	while d['2'] < n // 3:
		if d[arr[j]] > n // 3:
			d[arr[j]] -= 1
			d['2'] += 1
			arr[j] = '2'
		j -= 1
if d['1'] < n // 3:
	j = 0
	while d['1'] < n // 3 and j != n:
		if d[arr[j]] > n // 3 and arr[j] == '2':
			d[arr[j]] -= 1
			d['1'] += 1
			arr[j] = '1'
		j += 1
	j = n - 1
	while d['1'] < n // 3:
		if d[arr[j]] > n // 3 and arr[j] == '0':
			d[arr[j]] -= 1
			d['1'] += 1
			arr[j] = '1'
		j -= 1
print(''.join(arr))


n = int(input()) // 3
s = list(input())
_0 = s.count('0')
__0 = 0
_1 = s.count('1')
__1 = 0
_2 = s.count('2')
__2 = 0
for i in range(len(s)):
	if s[i] == '0':
		__0 += 1
	elif s[i] == '1':
		__1 += 1
	elif s[i] == '2':
		__2 += 1
	if s[i] == '0' and __0 > n and (_0 > n):
		if _1 < n:
			s[i] = '1'
			_1 += 1
			_0 -= 1
		elif _2 < n:
			s[i] = '2'
			_2 += 1
			_0 -= 1
	elif s[i] == '1' and _1 > n:
		if _0 < n:
			s[i] = '0'
			_0 += 1
			_1 -= 1
			__1 -= 1
		elif _2 < n and __1 > n:
			s[i] = '2'
			_2 += 1
			_1 -= 1
	elif s[i] == '2' and _2 > n:
		if _0 < n:
			s[i] = '0'
			_0 += 1
			_2 -= 1
		elif _1 < n:
			s[i] = '1'
			_1 += 1
			_2 -= 1
print(''.join(s))


N = int(input())
S = list(input())
(Zero, One, Two) = (S.count('0'), S.count('1'), S.count('2'))
i = 0
while i < N and Zero < N // 3:
	if S[i] == '1' and One > N // 3:
		S[i] = '0'
		Zero += 1
		One -= 1
	elif S[i] == '2' and Two > N // 3:
		S[i] = '0'
		Zero += 1
		Two -= 1
	i += 1
i = N - 1
while i >= 0 and Two < N // 3:
	if S[i] == '0' and Zero > N // 3:
		S[i] = '2'
		Two += 1
		Zero -= 1
	elif S[i] == '1' and One > N // 3:
		S[i] = '2'
		Two += 1
		One -= 1
	i -= 1
i = 0
while i < N and One < N // 3:
	if S[i] == '2' and Two > N // 3:
		S[i] = '1'
		One += 1
		Two -= 1
	i += 1
i = N - 1
while i >= 0 and One < N // 3:
	if S[i] == '0' and Zero > N // 3:
		S[i] = '1'
		One += 1
		Zero -= 1
	i -= 1
print(''.join(S))


n = int(input())
l = list(map(int, list(input())))
zeros = l.count(0)
ones = l.count(1)
twos = l.count(2)
ans = 0
if zeros < n // 3:
	for i in range(n):
		if zeros == n // 3:
			continue
		elif l[i] == 1 and n // 3 < ones:
			l[i] = 0
			ones -= 1
			zeros += 1
		elif l[i] == 2 and n // 3 < twos:
			l[i] = 0
			twos -= 1
			zeros += 1
stop = 0
for i in range(n):
	if l[i] == 0:
		stop += 1
	if stop == n // 3:
		stop = i
		break
if ones < n // 3:
	for i in range(n):
		if ones == n // 3:
			continue
		elif l[i] == 0 and i > stop and (n // 3 < zeros):
			l[i] = 1
			ones += 1
			zeros -= 1
		elif l[i] == 2 and n // 3 < twos:
			l[i] = 1
			ans += 1
			twos -= 1
			ones += 1
if twos < n // 3:
	for i in range(n - 1, -1, -1):
		if l[i] != 2 and n // 3 > twos:
			if l[i] == 0 and n // 3 < zeros:
				l[i] = 2
				twos += 1
				zeros -= 1
			elif l[i] == 1 and n // 3 < ones:
				l[i] = 2
				twos += 1
				ones -= 1
print(''.join(map(str, l)))


import copy
n = int(input())
s = list(input().strip())
zeros = s.count('0')
ones = s.count('1')
twos = s.count('2')
if zeros == ones and ones == twos:
	print(''.join(s))
else:
	q = n // 3
	zerosLeft = max(0, q - zeros)
	onesLeft = max(0, q - ones)
	twosLeft = max(0, q - twos)
	twosToBeReplaced = max(0, twos - q)
	onesToBeReplaced = max(0, ones - q)
	zerosToBeReplaced = max(0, zeros - q)
	cnt = [0, 0, 0]
	for i in range(n):
		c = s[i]
		if c == '0':
			if cnt[0] < q:
				cnt[0] += 1
				continue
			elif cnt[0] == q:
				if zerosToBeReplaced > 0:
					if onesLeft > 0:
						s[i] = '1'
						zerosToBeReplaced -= 1
						onesLeft -= 1
					elif twosLeft > 0:
						s[i] = '2'
						zerosToBeReplaced -= 1
						onesLeft -= 1
		elif c == '1':
			if onesToBeReplaced > 0:
				if zerosLeft > 0:
					s[i] = '0'
					onesToBeReplaced -= 1
					zerosLeft -= 1
				elif twosLeft > 0:
					if cnt[1] < q:
						cnt[1] += 1
						continue
					elif cnt[1] == q:
						s[i] = '2'
						onesToBeReplaced -= 1
						twosLeft -= 1
		elif c == '2':
			if twosToBeReplaced > 0:
				if zerosLeft > 0:
					s[i] = '0'
					twosToBeReplaced -= 1
					zerosLeft -= 1
				elif onesLeft > 0:
					s[i] = '1'
					twosToBeReplaced -= 1
					onesLeft -= 1
	print(''.join(s))


n = int(input().strip())
a = list(input().strip())
c = [0, 0, 0]
d = {}
d[0] = []
d[1] = []
d[2] = []
for i in a:
	if i == '0':
		c[0] += 1
		d[0].append(i)
	elif i == '1':
		c[1] += 1
		d[1].append(i)
	else:
		c[2] += 1
		d[2].append(i)
p = n // 3
r01 = 0
r10 = 0
r12 = 0
r21 = 0
r02 = 0
r20 = 0
if c[0] > p:
	if c[1] >= p:
		r02 = c[0] - p
		r12 = c[1] - p
	elif c[2] >= p:
		r01 = c[0] - p
		r21 = c[2] - p
	else:
		r01 = abs(c[1] - p)
		r02 = abs(c[2] - p)
elif c[0] == p:
	if c[1] >= p:
		r12 = abs(c[2] - p)
	else:
		r21 = abs(c[1] - p)
elif c[1] >= p:
	if c[2] >= p:
		r10 = abs(c[1] - p)
		r20 = abs(c[2] - p)
	else:
		r10 = abs(c[0] - p)
		r12 = abs(c[2] - p)
else:
	r20 = abs(c[0] - p)
	r21 = abs(c[1] - p)
vis = [0] * n
for i in range(n):
	if vis[i] == 0 and a[i] == '1' and (r10 > 0):
		vis[i] = 1
		a[i] = '0'
		r10 -= 1
	elif vis[i] == 0 and a[i] == '2' and (r20 > 0):
		vis[i] = 1
		a[i] = '0'
		r20 -= 1
for i in range(n):
	if vis[i] == 0 and a[i] == '2' and (r21 > 0):
		vis[i] = 1
		a[i] = '1'
		r21 -= 1
for i in range(n):
	j = n - i - 1
	if vis[j] == 0 and a[j] == '0' and (r02 > 0):
		vis[j] = 1
		a[j] = '2'
		r02 -= 1
	elif vis[j] == 0 and a[j] == '1' and (r12 > 0):
		vis[j] = 1
		a[j] = '2'
		r12 -= 1
for i in range(n):
	j = n - i - 1
	if vis[j] == 0 and a[j] == '0' and (r01 > 0):
		vis[j] = 1
		a[j] = '1'
		r01 -= 1
print(''.join(a))


from collections import Counter, OrderedDict
from itertools import permutations as perm
from fractions import Fraction
from collections import deque
from sys import stdin
from bisect import *
from heapq import *
from math import *
g = lambda : stdin.readline().strip()
gl = lambda : g().split()
gil = lambda : [int(var) for var in gl()]
gfl = lambda : [float(var) for var in gl()]
gcl = lambda : list(g())
gbs = lambda : [int(var) for var in g()]
mod = int(1000000000.0) + 7
inf = float('inf')
(n,) = gil()
s = gbs()
f = [0, s.count(1) - n // 3, s.count(2) - n // 3]
f[0] = -sum(f)
for i in range(n):
	if f[s[i]] > 0:
		for j in range(3):
			if f[j] < 0 and j < s[i]:
				f[j] += 1
				f[s[i]] -= 1
				s[i] = j
				break
for i in reversed(range(n)):
	if f[s[i]] > 0:
		for j in reversed(range(3)):
			if f[j] < 0:
				f[j] += 1
				f[s[i]] -= 1
				s[i] = j
				break
print(*s, sep='')


import math
n = int(input())
s = input()
f = [0] * 3
for i in s:
	if i == '0':
		f[0] += 1
	if i == '1':
		f[1] += 1
	if i == '2':
		f[2] += 1
rr = n // 3
arr = []
for i in s:
	arr.append(i)
for i in range(n):
	cur = int(s[i])
	if f[cur] > rr:
		for j in range(0, cur):
			if f[j] < rr:
				arr[i] = str(j)
				f[j] += 1
				f[cur] -= 1
				break
for i in range(n - 1, -1, -1):
	cur = int(s[i])
	if f[cur] > rr:
		for j in range(2, -1, -1):
			if j == cur:
				continue
			if f[j] < rr:
				arr[i] = str(j)
				f[j] += 1
				f[cur] -= 1
				break
print(''.join(arr))


N = int(input())
S = [str(i) for i in input()]
L = [0, 0, 0]
for s in S:
	L[int(s)] += 1
num = N // 3
T = S
(a, b, c) = (L[0], L[1], L[2])
if a >= num and b >= num and (c <= num):
	t = num - c
	s = a - num
	u = b - num
	i = N - 1
	while t > 0:
		if T[i] == '1' and u != 0:
			T[i] = '2'
			t -= 1
			u -= 1
		elif T[i] == '0' and s != 0:
			T[i] = '2'
			t -= 1
			s -= 1
		i -= 1
elif a >= num and b <= num and (c <= num):
	t = num - c
	i = N - 1
	while t > 0:
		if T[i] == '0':
			T[i] = '2'
			t -= 1
		i -= 1
	s = num - b
	while s > 0:
		if T[i] == '0':
			T[i] = '1'
			s -= 1
		i -= 1
elif a >= num and b <= num and (c >= num):
	t = c - num
	i = 0
	while t > 0:
		if T[i] == '2':
			T[i] = '1'
			t -= 1
		i += 1
	i = N - 1
	s = a - num
	while s > 0:
		if T[i] == '0':
			T[i] = '1'
			s -= 1
		i -= 1
elif a <= num and b >= num and (c <= num):
	t = num - a
	i = 0
	while t > 0:
		if T[i] == '1':
			T[i] = '0'
			t -= 1
		i += 1
	i = N - 1
	s = num - c
	while s > 0:
		if T[i] == '1':
			T[i] = '2'
			s -= 1
		i -= 1
elif a <= num and b <= num and (c >= num):
	t = num - a
	i = 0
	while t > 0:
		if T[i] == '2':
			T[i] = '0'
			t -= 1
		i += 1
	s = num - b
	while s > 0:
		if T[i] == '2':
			T[i] = '1'
			s -= 1
		i += 1
elif a <= num and b >= num and (c >= num):
	t = num - a
	s = b - num
	u = c - num
	i = 0
	while t > 0:
		if T[i] == '2' and u != 0:
			T[i] = '0'
			t -= 1
			u -= 1
		elif T[i] == '1' and s != 0:
			T[i] = '0'
			t -= 1
			s -= 1
		i += 1
print(''.join(T))


MOD = 10 ** 9 + 7
I = lambda : list(map(int, input().split()))
(n,) = I()
s = list(input())
c = [0] * 3
temp = s[:]
for i in s:
	c[int(i)] += 1
if c[2] < n // 3:
	diff = n // 3 - c[2]
	if c[1] > n // 3 and c[2] > n // 3:
		for i in range(n - 1, -1, -1):
			if diff and s[i] == '1' and (c[1] > n // 3):
				s[i] = '2'
				c[1] -= 1
				diff -= 1
				c[2] += 1
			if diff and s[i] == '0' and (c[0] > n // 3):
				s[i] = '2'
				c[0] -= 1
				diff -= 1
				c[2] += 1
	if c[1] > n // 3:
		for i in range(n - 1, -1, -1):
			if diff and s[i] == '1' and (c[1] > n // 3):
				s[i] = '2'
				c[1] -= 1
				diff -= 1
				c[2] += 1
	if c[0] > n // 3:
		for i in range(n - 1, -1, -1):
			if diff and s[i] == '0' and (c[0] > n // 3):
				s[i] = '2'
				c[0] -= 1
				diff -= 1
				c[2] += 1
if c[0] < n // 3:
	diff = n // 3 - c[0]
	if c[1] > n // 3 and c[2] > n // 3:
		for i in range(n):
			if diff and s[i] == '1' and (c[1] > n // 3):
				s[i] = '0'
				c[1] -= 1
				diff -= 1
				c[0] += 1
			elif diff and s[i] == '2' and (c[2] > n // 3):
				s[i] = '0'
				c[2] -= 1
				diff -= 1
				c[0] += 1
	elif c[2] > n // 3:
		for i in range(n):
			if diff and s[i] == '2' and (c[2] > n // 3):
				s[i] = '0'
				c[2] -= 1
				diff -= 1
				c[0] += 1
	elif c[1] > n // 3:
		for i in range(n):
			if diff and s[i] == '1' and (c[1] > n // 3):
				s[i] = '0'
				c[1] -= 1
				diff -= 1
				c[0] += 1
if c[1] < n // 3:
	diff = n // 3 - c[1]
	if c[2] > n // 3:
		for i in range(n):
			if diff and s[i] == '2' and (c[2] > n // 3):
				s[i] = '1'
				c[2] -= 1
				diff -= 1
				c[1] += 1
	if c[0] > n // 3:
		for i in range(n - 1, -1, -1):
			if diff and s[i] == '0' and (c[0] > n // 3):
				s[i] = '1'
				c[0] -= 1
				diff -= 1
				c[1] += 1
print(''.join(s))


n = int(input())
s1 = str(input())
count1 = s1.count('0')
count2 = s1.count('1')
count3 = s1.count('2')
s = []
for i in s1:
	s.append(i)
finish = n // 3
tcount1 = 0
tcount2 = 0
tcount3 = 0
for i in range(n):
	if s[i] == '0':
		if tcount1 == n // 3:
			if count2 < finish and tcount2 < finish:
				s[i] = '1'
				count2 += 1
				tcount2 += 1
				count1 -= 1
			elif count3 < finish and tcount3 < finish:
				s[i] = '2'
				count3 += 1
				tcount3 += 1
				count1 -= 1
			else:
				tcount1 += 1
		else:
			tcount1 += 1
	elif s[i] == '1':
		if count2 > n // 3:
			if count1 < n // 3 and tcount1 < n // 3:
				s[i] = '0'
				count1 += 1
				tcount1 += 1
				count2 -= 1
			elif tcount2 == n // 3:
				s[i] = '2'
				count3 += 1
				tcount3 += 1
				count2 -= 1
			else:
				tcount2 += 1
		else:
			tcount2 += 1
	elif count3 > n // 3:
		if count1 < n // 3 and tcount1 < n // 3:
			s[i] = '0'
			count1 += 1
			tcount1 += 1
			count3 -= 1
		elif count2 < n // 3 and tcount2 < n // 3:
			s[i] = '1'
			count2 += 1
			tcount2 += 1
			count3 -= 1
		else:
			tcount3 += 1
	else:
		tcount3 += 1
ans = ''
for i in s:
	ans += i
print(ans)


n = int(input())
s = [int(x) for x in input()]
zeros = s.count(0)
ones = s.count(1)
twos = s.count(2)
target = len(s) // 3
if zeros < target:
	for i in range(n):
		if s[i] == 2 and twos > target:
			s[i] = 0
			zeros += 1
			twos -= 1
		if s[i] == 1 and ones > target:
			s[i] = 0
			zeros += 1
			ones -= 1
		if zeros >= target:
			break
if ones < target:
	for i in range(n):
		if s[i] == 2 and twos > target:
			s[i] = 1
			ones += 1
			twos -= 1
		if ones >= target:
			break
if twos < target:
	for i in range(n):
		if s[-i - 1] == 0 and zeros > target:
			s[-i - 1] = 2
			twos += 1
			zeros -= 1
		if s[-i - 1] == 1 and ones > target:
			s[-i - 1] = 2
			twos += 1
			ones -= 1
		if twos >= target:
			break
if ones < target:
	for i in range(n):
		if s[-i - 1] == 0 and zeros > target:
			s[-i - 1] = 1
			ones += 1
			zeros -= 1
		if ones >= target:
			break
assert zeros == ones
assert ones == twos
assert twos == target
print(''.join((str(x) for x in s)))


n = int(input())
s = input()
c = [0] * 3
for i in ['0', '1', '2']:
	c[int(i)] = s.count(i)
td = [0] * 3
ndiv3 = n // 3
for i in range(3):
	td[i] = ndiv3 - c[i]
md = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
for f in range(3):
	for t in range(3):
		if td[f] < 0 and td[t] > 0:
			md[f][t] = min(abs(td[f]), abs(td[t]))
for (f, t) in (('0', '2'), ('2', '0'), ('0', '1'), ('1', '0'), ('1', '2'), ('2', '1')):
	if f > t:
		s = s.replace(f, t, md[int(f)][int(t)])
	else:
		s = s[::-1].replace(f, t, md[int(f)][int(t)])[::-1]
print(s)


def ia():
	n = input().split()
	return list(map(int, n))

def ic():
	return list(input())

def ii():
	return int(input())
n = int(ii())
k = int(n / 3)
s = ic()
from collections import Counter
d = Counter(s)
a = d['0']
b = d['1']
c = d['2']
if a == k and b == k and (c == k):
	print(''.join(s))
else:
	if c < k:
		if b > k:
			for i in range(n - 1, 0, -1):
				if s[i] == '1':
					s[i] = '2'
					c += 1
					b -= 1
				if b == k:
					break
				if c == k:
					break
		if a > k:
			for i in range(n - 1, 0, -1):
				if s[i] == '0':
					s[i] = '2'
					c += 1
					a -= 1
				if c == k:
					break
				if a == k:
					break
	if a < k:
		if c > k:
			for i in range(n):
				if s[i] == '2':
					s[i] = '0'
					a += 1
					c -= 1
				if c == k:
					break
				if a == k:
					break
		if b > k:
			for i in range(n):
				if s[i] == '1':
					s[i] = '0'
					a += 1
					b -= 1
				if b == k:
					break
				if a == k:
					break
	if b < k:
		if c > k:
			for i in range(n):
				if s[i] == '2':
					s[i] = '1'
					b += 1
					c -= 1
				if b == k:
					break
				if c == k:
					break
		if a > k:
			for i in range(n - 1, 0, -1):
				if s[i] == '0':
					s[i] = '1'
					b += 1
					a -= 1
				if b == k:
					break
				if a == k:
					break
	print(''.join(s))


n = int(input())
s = [i for i in input()]
d = [0] * 3
for i in s:
	d[int(i)] += 1
v = n // 3
i = 0
if d[2] < v:
	i = n - 1
	while i >= 0 and d[2] != v and (d[1] > v):
		if s[i] == '1':
			d[1] -= 1
			d[2] += 1
			s[i] = '2'
		i -= 1
	i = n - 1
	while i >= 0 and d[2] != v and (d[0] > v):
		if s[i] == '0':
			d[0] -= 1
			d[2] += 1
			s[i] = '2'
		i -= 1
if d[0] < v:
	i = 0
	while i < n and d[0] != v and (d[2] > v):
		if s[i] == '2':
			d[2] -= 1
			d[0] += 1
			s[i] = '0'
		i += 1
	i = 0
	while i >= 0 and d[0] != v and (d[1] > v):
		if s[i] == '1':
			d[1] -= 1
			d[0] += 1
			s[i] = '0'
		i += 1
if d[1] < v:
	i = 0
	while i < n and d[1] != v and (d[2] > v):
		if s[i] == '2':
			d[2] -= 1
			d[1] += 1
			s[i] = '1'
		i += 1
	i = n - 1
	while i >= 0 and d[1] != v and (d[0] > v):
		if s[i] == '0':
			d[0] -= 1
			d[1] += 1
			s[i] = '1'
		i -= 1
print(''.join(s))


n = int(input())
string = list(map(int, list(input())))
req_cnt = n // 3
cnts = [0, 0, 0]
for s in string:
	cnts[s] += 1
if cnts[0] < req_cnt:
	if cnts[1] > req_cnt:
		to_change = min(req_cnt - cnts[0], cnts[1] - req_cnt)
		(i, cnts[0], cnts[1]) = (0, cnts[0] + to_change, cnts[1] - to_change)
		while to_change != 0:
			if string[i] == 1:
				string[i] = 0
				to_change -= 1
			i += 1
	if cnts[2] > req_cnt:
		to_change = min(req_cnt - cnts[0], cnts[2] - req_cnt)
		(i, cnts[0], cnts[2]) = (0, cnts[0] + to_change, cnts[2] - to_change)
		while to_change != 0:
			if string[i] == 2:
				string[i] = 0
				to_change -= 1
			i += 1
if cnts[2] < req_cnt:
	if cnts[0] > req_cnt:
		to_change = min(req_cnt - cnts[2], cnts[0] - req_cnt)
		(i, cnts[2], cnts[0]) = (n - 1, cnts[2] + to_change, cnts[0] - to_change)
		while to_change != 0:
			if string[i] == 0:
				string[i] = 2
				to_change -= 1
			i -= 1
	if cnts[1] > req_cnt:
		to_change = min(req_cnt - cnts[2], cnts[1] - req_cnt)
		(i, cnts[2], cnts[1]) = (n - 1, cnts[2] + to_change, cnts[1] - to_change)
		while to_change != 0:
			if string[i] == 1:
				string[i] = 2
				to_change -= 1
			i -= 1
if cnts[1] < req_cnt:
	if cnts[0] > req_cnt:
		to_change = min(req_cnt - cnts[1], cnts[0] - req_cnt)
		(i, cnts[1], cnts[0]) = (n - 1, cnts[1] + to_change, cnts[0] - to_change)
		while to_change != 0:
			if string[i] == 0:
				string[i] = 1
				to_change -= 1
			i -= 1
	if cnts[2] > req_cnt:
		to_change = min(req_cnt - cnts[1], cnts[2] - req_cnt)
		(i, cnts[1], cnts[2]) = (0, cnts[1] + to_change, cnts[2] - to_change)
		while to_change != 0:
			if string[i] == 2:
				string[i] = 1
				to_change -= 1
			i += 1
print(''.join(map(str, string)))


n = int(input())
m = n // 3
s = input()
a = []
for i in s:
	a.append(int(i))
cnt = [0, 0, 0]
for i in a:
	cnt[i] += 1
for i in range(3):
	cntt = [0, 0, 0]
	ptr = 0
	while cnt[i] < m:
		if cnt[a[ptr]] > m:
			if i == 0:
				cnt[i] += 1
				cnt[a[ptr]] -= 1
				a[ptr] = i
			if i == 1 and (a[ptr] == 0 and cntt[0] >= m or a[ptr] != 0):
				cnt[i] += 1
				cnt[a[ptr]] -= 1
				a[ptr] = i
			if i == 2 and (a[ptr] == 0 and cntt[0] >= m or (a[ptr] == 1 and cntt[1] >= m)):
				cnt[i] += 1
				cnt[a[ptr]] -= 1
				a[ptr] = i
		cntt[a[ptr]] += 1
		ptr += 1
print(*a, sep='')


from collections import defaultdict

def solve():
	n = int(input())
	s = list(input())
	freq = defaultdict(int)
	for i in range(n):
		freq[s[i]] += 1
	zero_cnt = 0
	one_cnt = 0
	for i in range(n):
		if s[i] == '0' and zero_cnt < n // 3:
			zero_cnt += 1
			continue
		if freq['0'] < n // 3:
			if freq[s[i]] > n // 3:
				freq[s[i]] -= 1
				freq['0'] += 1
				s[i] = '0'
				continue
		if s[i] == '1' and one_cnt < n // 3:
			one_cnt += 1
			continue
		if freq['1'] < n // 3:
			if freq[s[i]] > n // 3:
				freq[s[i]] -= 1
				freq['1'] += 1
				s[i] = '1'
				continue
		if freq['2'] < n // 3:
			if freq[s[i]] > n // 3:
				freq[s[i]] -= 1
				freq['2'] += 1
				s[i] = '2'
				continue
	print(''.join((k for k in s)))
solve()


from sys import stdin

def find_freq(string, n):
	zero = 0
	one = 0
	two = 0
	start = 0
	for i in range(start, n):
		if string[i] == '0':
			zero += 1
		elif string[i] == '1':
			one += 1
		else:
			two += 1
	return (zero, one, two)

def find_start(symbol, victim, string, n):
	if symbol == '0':
		return 0
	elif symbol == '1':
		if victim == '0':
			zero = 0
			for i in range(n):
				if zero == n // 3:
					return i
				if string[i] == '0':
					zero += 1
		if victim == '2':
			return 0
	elif symbol == '2':
		if victim == '0':
			zero = 0
			for i in range(n):
				if zero == n // 3:
					return i
				if string[i] == '0':
					zero += 1
		elif victim == '1':
			one = 0
			for i in range(n):
				if one == n // 3:
					return i
				if string[i] == '1':
					one += 1
	return 0

def make_zeroes(string, n, zero, one, two):
	if two > n // 3 and zero < n // 3:
		start = find_start('0', '2', string, n)
		for i in range(start, n):
			if two <= n // 3 or zero == n // 3:
				break
			if string[i] == '2':
				string[i] = '0'
				two -= 1
				zero += 1
	if one > n // 3 and zero < n // 3:
		start = find_start('0', '1', string, n)
		for i in range(start, n):
			if one <= n // 3 or zero == n // 3:
				break
			if string[i] == '1':
				string[i] = '0'
				one -= 1
				zero += 1
	return (zero, one, two)

def make_ones(string, n, zero, one, two):
	if two > n // 3 and one < n // 3:
		start = find_start('1', '2', string, n)
		for i in range(start, n):
			if two <= n // 3 or one == n // 3:
				break
			if string[i] == '2':
				string[i] = '1'
				two -= 1
				one = 1
	if zero > n // 3 and one < n // 3:
		start = find_start('1', '0', string, n)
		for i in range(start, n):
			if zero <= n // 3 or one == n // 3:
				break
			if string[i] == '0':
				string[i] = '1'
				zero -= 1
				one += 1
	return (zero, one, two)

def make_twos(string, n, zero, one, two):
	if one > n // 3 and two < n // 3:
		start = find_start('2', '1', string, n)
		for i in range(start, n):
			if one <= n // 3 or two == n // 3:
				break
			if string[i] == '1':
				string[i] = '2'
				one -= 1
				two += 1
	if zero > n // 3 and two < n // 3:
		start = find_start('2', '0', string, n)
		for i in range(start, n):
			if zero <= n // 3 or two == n // 3:
				break
			if string[i] == '0':
				string[i] = '2'
				zero -= 1
				two += 1
	return (zero, one, two)
n = int(stdin.readline().strip())
string = list(stdin.readline().strip())
(zero, one, two) = find_freq(string, n)
(zero, one, two) = make_zeroes(string, n, zero, one, two)
(zero, one, two) = make_ones(string, n, zero, one, two)
(zero, one, two) = make_twos(string, n, zero, one, two)
for i in range(n):
	print(string[i], end='')


def new_print(arr):
	print(''.join(arr))
n = int(input())
str = input()
l0 = str.count('0')
l1 = str.count('1')
l2 = str.count('2')
str = list(str)
real = n // 3
cur = 0
while l0 < real:
	if str[cur] == '1' and l1 > real:
		l1 -= 1
		l0 += 1
		str[cur] = '0'
	if str[cur] == '2' and l2 > real:
		l2 -= 1
		l0 += 1
		str[cur] = '0'
	cur += 1
cur = 0
while l1 < real and cur < len(str) and (l2 > real):
	if str[cur] == '2':
		l2 -= 1
		l1 += 1
		str[cur] = '1'
	cur += 1
cur = len(str) - 1
while l2 < real and cur >= 0:
	if str[cur] == '1' and l1 > real:
		l2 += 1
		l1 -= 1
		str[cur] = '2'
	if str[cur] == '0' and l0 > real:
		l2 += 1
		l0 -= 1
		str[cur] = '2'
	cur -= 1
cur = len(str) - 1
while l1 < real and cur >= 0:
	if str[cur] == '0' and l0 > real:
		l1 += 1
		l0 -= 1
		str[cur] = '1'
	cur -= 1
new_print(str)


import sys
input = sys.stdin.readline
n = int(input())
S = input().strip()
Z = 0
O = 0
T = 0
for s in S:
	if s == '0':
		Z += 1
	elif s == '1':
		O += 1
	else:
		T += 1
ZC = Z - n // 3
OC = O - n // 3
TC = T - n // 3
ANS = [None] * n
if ZC == OC == TC == 0:
	print(S)
	sys.exit()
elif ZC > 0 and OC > 0:
	for i in range(n - 1, -1, -1):
		if S[i] == '0' and ZC > 0:
			ANS[i] = 2
			ZC -= 1
		elif S[i] == '1' and OC > 0:
			ANS[i] = 2
			OC -= 1
		else:
			ANS[i] = S[i]
elif OC > 0 and TC > 0:
	for i in range(n):
		if S[i] == '1' and OC > 0:
			ANS[i] = 0
			OC -= 1
		elif S[i] == '2' and TC > 0:
			ANS[i] = 0
			TC -= 1
		else:
			ANS[i] = S[i]
elif ZC > 0 and TC > 0:
	for i in range(n):
		if S[i] == '2' and TC > 0:
			ANS[i] = 1
			TC -= 1
	for i in range(n - 1, -1, -1):
		if S[i] == '0' and ZC > 0:
			ANS[i] = 1
			ZC -= 1
	for i in range(n):
		if ANS[i] == None:
			ANS[i] = S[i]
elif ZC > 0 and OC <= 0 and (TC <= 0):
	for i in range(n - 1, -1, -1):
		if S[i] == '0' and TC < 0:
			ANS[i] = 2
			TC += 1
		elif S[i] == '0' and OC < 0:
			ANS[i] = 1
			OC += 1
		else:
			ANS[i] = S[i]
elif TC > 0 and ZC <= 0 and (OC <= 0):
	for i in range(n):
		if S[i] == '2' and ZC < 0:
			ANS[i] = 0
			ZC += 1
		elif S[i] == '2' and OC < 0:
			ANS[i] = 1
			OC += 1
		else:
			ANS[i] = S[i]
elif OC > 0 and ZC <= 0 and (TC <= 0):
	for i in range(n):
		if S[i] == '1' and ZC < 0:
			ANS[i] = 0
			ZC += 1
	for i in range(n - 1, -1, -1):
		if S[i] == '1' and TC < 0:
			ANS[i] = 2
			TC += 1
	for i in range(n):
		if ANS[i] == None:
			ANS[i] = S[i]
for a in ANS:
	print(a, end='')


def mi():
	return list(map(int, input().split()))
n = int(input())
a = list(input())
(zc, oc, tc) = (0, 0, 0)
for i in a:
	if i == '1':
		oc += 1
	elif i == '0':
		zc += 1
	else:
		tc += 1
if oc == tc and oc == zc:
	print(''.join(a))
	return
target = n // 3
ztba = max(0, target - zc)
ztbr = max(0, zc - target)
otba = max(0, target - oc)
otbr = max(0, oc - target)
ttba = max(0, target - tc)
ttbr = max(0, tc - target)
if ztbr:
	for i in range(n - 1, -1, -1):
		if a[i] == '0':
			ztbr -= 1
			if ttba:
				a[i] = '2'
				ttba -= 1
			elif otba:
				a[i] = '1'
				otba -= 1
		if ztbr == 0:
			break
if otbr:
	if ztba:
		for i in range(n):
			if otbr == 0:
				break
			if a[i] == '1':
				a[i] = '0'
				otbr -= 1
				ztba -= 1
			if ztba == 0:
				break
	if otbr and ttba:
		for i in range(n - 1, -1, -1):
			if otbr == 0:
				break
			if a[i] == '1':
				a[i] = '2'
				otbr -= 1
				ttba -= 1
			if ttba == 0:
				break
if ttbr:
	if ztba:
		for i in range(n):
			if ttbr == 0:
				break
			if a[i] == '2':
				a[i] = '0'
				ttbr -= 1
				ztba -= 1
			if ztba == 0:
				break
	if ttbr and otba:
		for i in range(n):
			if ttbr == 0:
				break
			if a[i] == '2':
				a[i] = '1'
				ttbr -= 1
				otba -= 1
			if otba == 0:
				break
print(''.join(a))


n = int(input())
row = input()
totals = [row.count('0'), row.count('1'), row.count('2')]
flows = []
for i in range(3):
	flows.append([0, 0, 0])
goal = n // 3
goals = [t - goal for t in totals]
for (i, g) in enumerate(goals):
	if goals[i] > 0:
		for (j, g2) in enumerate(goals):
			if g2 < 0:
				flows[i][j] = min(goals[i], abs(g2))
				goals[i] -= flows[i][j]
				goals[j] += flows[i][j]
new_row = list(row)
ost = flows[0][2]
for (i, s) in enumerate(reversed(new_row)):
	i = (i + 1) * -1
	if ost:
		if s == '0':
			new_row[i] = '2'
			ost -= 1
	else:
		break
ost = flows[0][1]
for (i, s) in enumerate(reversed(new_row)):
	i = (i + 1) * -1
	if ost:
		if s == '0':
			new_row[i] = '1'
			ost -= 1
	else:
		break
ost = flows[1][2]
for (i, s) in enumerate(reversed(new_row)):
	i = (i + 1) * -1
	if ost:
		if s == '1':
			new_row[i] = '2'
			ost -= 1
	else:
		break
ost = flows[2][0]
for (i, s) in enumerate(new_row):
	if ost:
		if s == '2':
			new_row[i] = '0'
			ost -= 1
	else:
		break
ost = flows[2][1]
for (i, s) in enumerate(new_row):
	if ost:
		if s == '2':
			new_row[i] = '1'
			ost -= 1
	else:
		break
ost = flows[1][0]
for (i, s) in enumerate(new_row):
	if ost:
		if s == '1':
			new_row[i] = '0'
			ost -= 1
	else:
		break
print(''.join(new_row))


n = int(input())
x = input()
a = x.count('0')
b = x.count('1')
c = x.count('2')
s = list((c for c in x))
if a > n // 3:
	if c < n // 3:
		for i in range(len(s) - 1, -1, -1):
			if s[i] == '0':
				s[i] = '2'
				a -= 1
				c += 1
			if c == n // 3:
				break
			if a == n // 3:
				break
	if a > n // 3:
		if b < n // 3:
			for i in range(len(s) - 1, -1, -1):
				if s[i] == '0':
					s[i] = '1'
					a -= 1
					b += 1
				if b == n // 3:
					break
				if a == n // 3:
					break
if b > n // 3:
	if a < n // 3:
		for i in range(0, len(s)):
			if s[i] == '1':
				s[i] = '0'
				a += 1
				b -= 1
			if b == n // 3:
				break
			if a == n // 3:
				break
	if b > n // 3:
		if c < n // 3:
			for i in range(len(s) - 1, -1, -1):
				if s[i] == '1':
					s[i] = '2'
					b -= 1
					c += 1
				if b == n // 3:
					break
				if c == n // 3:
					break
if c > n // 3:
	if a < n // 3:
		for i in range(0, len(s)):
			if s[i] == '2':
				s[i] = '0'
				a += 1
				c -= 1
			if c == n // 3:
				break
			if a == n // 3:
				break
	if c > n // 3:
		if b < n // 3:
			for i in range(0, len(s)):
				if s[i] == '2':
					s[i] = '1'
					b += 1
					c -= 1
				if b == n // 3:
					break
				if c == n // 3:
					break
print(''.join(s))


n = int(input())
inp1 = input()
inp = []
for i in range(n):
	inp.append(str(inp1[i]))
c1 = 0
c2 = 0
c0 = 0
req = n / 3
for i in range(n):
	if inp[i] == '0':
		c0 += 1
	elif inp[i] == '1':
		c1 += 1
	elif inp[i] == '2':
		c2 += 1
if c1 == c2 and c2 == c0:
	print(''.join(inp))
else:
	if c2 < req:
		for i in range(n - 1, -1, -1):
			if c2 == req:
				break
			if inp[i] != '2':
				if inp[i] == '1':
					if c1 > req:
						c1 -= 1
						inp[i] = '2'
						c2 += 1
				elif c0 > req:
					c0 -= 1
					inp[i] = '2'
					c2 += 1
	if c0 < req:
		for i in range(n):
			if c0 == req:
				break
			if inp[i] == '1':
				if c1 > req:
					c1 -= 1
					inp[i] = '0'
					c0 += 1
			elif inp[i] == '2':
				if c2 > req:
					c2 -= 1
					inp[i] = '0'
					c0 += 1
	if c1 < req:
		for i in range(n):
			if c1 == req:
				break
			if inp[i] == '2':
				if c2 > req:
					c2 -= 1
					inp[i] = '1'
					c1 += 1
				else:
					break
		for i in range(n - 1, -1, -1):
			if c1 == req:
				break
			if inp[i] == '0':
				if c0 > req:
					c0 -= 1
					inp[i] = '1'
					c1 -= 1
				else:
					break
	print(''.join(inp))


n = int(input())
a = list(map(int, list(input())))
mx = n // 3
count = [0 for _ in range(3)]
location = [[] for _ in range(3)]
aviliable = []
for i in range(n):
	count[a[i]] += 1
	location[a[i]].append(i)
need = mx - count[0]
for i in range(3):
	location[i].sort()
	count[i] = mx - count[i]
for i in range(3):
	if count[i] > 0:
		for j in range(i - 1, -1, -1):
			if count[j] < 0:
				m = 0
				hold = abs(count[j])
				if hold >= count[i]:
					for k in range(count[i]):
						a[location[j][mx + k]] = i
					count[j] += count[i]
					location[j] = location[j][:mx] + location[j][mx + count[i]:]
					count[i] = 0
					break
				else:
					for k in range(hold):
						a[location[j][mx + k]] = i
					count[j] = 0
					count[i] -= hold
		for j in range(i + 1, 3):
			if count[i] == 0:
				break
			if count[j] < 0:
				m = 0
				hold = abs(count[j])
				if hold >= count[i]:
					for k in range(count[i]):
						a[location[j][k]] = i
					count[j] += count[i]
					location[j] = location[j][count[i]:]
					count[i] = 0
					break
				else:
					for k in range(hold):
						a[location[j][k]] = i
					count[j] = 0
					count[i] -= hold
t = ''
for v in a:
	t += str(v)
print(t)


n = int(input())
s = str(input())
ls = list(s)
mp = {'2': 0, '1': 0, '0': 0}
for c in ls:
	mp[c] += 1
for (i, c) in enumerate(ls):
	if mp[ls[i]] > n / 3:
		for (k, v) in mp.items():
			if v < n / 3 and k < ls[i]:
				mp[ls[i]] -= 1
				ls[i] = k
				mp[k] += 1
for i in range(len(ls) - 1, 0, -1):
	if mp[ls[i]] > n / 3:
		for (k, v) in mp.items():
			if v < n / 3 and k > ls[i]:
				mp[ls[i]] -= 1
				ls[i] = k
				mp[k] += 1
print(''.join(ls))


n = int(input())
s = list(input())
if s.count('0') == s.count('1') and s.count('1') == s.count('2'):
	for i in range(n):
		print(s[i], end='')
else:
	a = s.count('0')
	b = s.count('1')
	c = s.count('2')
	e = a - n // 3
	f = b - n // 3
	g = c - n // 3
	if e > 0:
		if g < 0:
			for i in reversed(range(n)):
				if s[i] == '0' and g < 0 and (e != 0):
					s[i] = '2'
					g += 1
					e -= 1
		if f < 0:
			for i in reversed(range(n)):
				if s[i] == '0' and f < 0 and (e != 0):
					s[i] = '1'
					f += 1
					e -= 1
		if f > 0:
			for i in reversed(range(n)):
				if s[i] == '1' and g < 0 and (f != 0):
					s[i] = '2'
					g += 1
					f -= 1
		if g > 0:
			for i in range(n):
				if s[i] == '2' and f < 0 and (g != 0):
					s[i] = '1'
					f += 1
					g -= 1
	else:
		if g > 0:
			for i in range(n):
				if s[i] == '2' and e < 0 and (g != 0):
					s[i] = '0'
					e += 1
					g -= 1
			if f > 0:
				for i in range(n):
					if s[i] == '1' and e < 0 and (f != 0):
						s[i] = '0'
						e += 1
						f -= 1
			if f < 0:
				for i in range(n):
					if s[i] == '2' and f < 0 and (g != 0):
						s[i] = '1'
						f += 1
						g -= 1
		if f > 0:
			for i in range(n):
				if s[i] == '1' and e < 0 and (f != 0):
					s[i] = '0'
					e += 1
					f -= 1
			for i in reversed(range(n)):
				if s[i] == '1' and g < 0 and (f != 0):
					s[i] = '2'
					g += 1
					f -= 1
	for i in range(n):
		print(s[i], end='')


n = int(input())
s = list(map(int, list(input())))
ans = 0
z = s.count(0)
o = s.count(1)
t = s.count(2)
r = n // 3
if z >= r:
	ans += z - r
if o >= r:
	ans += o - r
if t >= r:
	ans += t - r
if z < r:
	c2 = max(0, t - r)
	c1 = max(0, o - r)
	for i in range(n):
		if z == r:
			break
		if s[i] == 0:
			continue
		elif s[i] == 2 and c2 > 0:
			s[i] = 0
			t -= 1
			c2 -= 1
			z += 1
		elif s[i] == 1 and c1 > 0:
			s[i] = 0
			o -= 1
			z += 1
			c1 -= 1
if t < r:
	c0 = max(0, z - r)
	c1 = max(0, o - r)
	for i in range(n - 1, 0, -1):
		if t == r:
			break
		if s[i] == 2:
			continue
		elif s[i] == 0 and c0 > 0:
			s[i] = 2
			t += 1
			c0 -= 1
			z -= 1
		elif s[i] == 1 and c1 > 0:
			s[i] = 2
			o -= 1
			t += 1
			c1 -= 1
if o < r:
	c0 = max(0, z - r)
	c2 = max(0, t - r)
	for i in range(n):
		if s[i] == 2 and c2 > 0:
			s[i] = 1
			c2 -= 1
	for i in range(n - 1, -1, -1):
		if s[i] == 0 and c0 > 0:
			s[i] = 1
			c0 -= 1
for i in s:
	print(i, end='')


from collections import Counter
N = int(input())
balanced = N // 3
s = list(map(int, list(input())))
counter = Counter(s)
if counter[0] < balanced:
	for (i, n) in enumerate(s):
		if n > 0 and counter[n] > balanced:
			s[i] = 0
			counter[n] -= 1
			counter[0] += 1
		if counter[0] == balanced:
			break
if counter[2] < balanced:
	for (i, n) in zip(range(N - 1, -1, -1), s[::-1]):
		if n != 2 and counter[n] > balanced:
			s[i] = 2
			counter[n] -= 1
			counter[2] += 1
		if counter[2] == balanced:
			break
if counter[1] < balanced:
	for (i, n) in enumerate(s):
		if n == 2 and counter[2] > balanced:
			s[i] = 1
			counter[2] -= 1
			counter[1] += 1
		if counter[1] == balanced:
			break
if counter[1] < balanced:
	for (i, n) in zip(range(N - 1, -1, -1), s[::-1]):
		if n == 0:
			s[i] = 1
			counter[1] += 1
		if counter[1] == balanced:
			break
print(*s, sep='')


n = int(input())
s = input()
lists = [int(char) for char in s]
(a, b) = (s.count('0'), s.count('1'))
c = n - a - b
(a, b, c) = (n // 3 - a, n // 3 - b, n // 3 - c)
if a > 0:
	for i in range(n):
		if a == 0:
			break
		if lists[i] == 1:
			if b < 0:
				lists[i] = 0
				b += 1
				a -= 1
		elif lists[i] == 2:
			if c < 0:
				lists[i] = 0
				c += 1
				a -= 1
if b > 0 and c < 0:
	for i in range(n):
		if b == 0 or c == 0:
			break
		if lists[i] == 2:
			lists[i] = 1
			b -= 1
			c += 1
if a < 0:
	for i in range(n):
		if a == 0:
			break
		if lists[-i - 1] == 0:
			if c > 0:
				lists[-i - 1] = 2
				c -= 1
				a += 1
			else:
				lists[-i - 1] = 1
				b -= 1
				a += 1
if b < 0:
	for i in range(n):
		if b == 0:
			break
		if lists[-i - 1] == 1:
			lists[-i - 1] = 2
			b += 1
			c -= 1
lists = [str(lists[i]) for i in range(n)]
print(''.join(lists))


def swap(s, to_, from_, count, skip_first):
	res = list(s)
	for i in range(len(s)):
		if s[i] == from_:
			if skip_first:
				skip_first -= 1
				continue
			res[i] = to_
			count -= 1
		if not count:
			break
	return ''.join(res)
n = int(input())
s = input()
counts = [0, 0, 0]
for c in s:
	counts[int(c)] += 1
true_count = n // 3
for (i, v) in enumerate(counts):
	if v < true_count:
		needed = true_count - v
		others = [0, 1, 2]
		others.remove(i)
		for j in others:
			if counts[j] > true_count:
				extra = counts[j] - true_count
				if extra > needed:
					to_swap = needed
					needed = 0
				else:
					to_swap = extra
					needed -= to_swap
				skip_first = 0
				if i > j:
					skip_first = true_count
				s = swap(s, str(i), str(j), to_swap, skip_first)
			if not needed:
				break
print(s)


n = int(input())
s = list(input())
a = []
b = []
c = []
for i in range(n):
	if s[i] == '0':
		a.append(i)
	elif s[i] == '1':
		b.append(i)
	else:
		c.append(i)
st = n // 3
x = len(a)
y = len(b)
z = len(c)
if x < st:
	i = 0
	while y > st and x < st:
		s[b[i]] = '0'
		i += 1
		x += 1
		y -= 1
	b = b[i:]
	i = 0
	while z > st and x < st:
		s[c[i]] = '0'
		i += 1
		x += 1
		z -= 1
	c = c[i:]
if z < st:
	while y > st and z < st:
		s[b[-1]] = '2'
		b.pop()
		z += 1
		y -= 1
	while x > st and z < st:
		s[a[-1]] = '2'
		a.pop()
		z += 1
		x -= 1
if y < st:
	while x > st and y < st:
		s[a[-1]] = '1'
		a.pop()
		y += 1
		x -= 1
	i = 0
	while z > st and y < st:
		s[c[i]] = '1'
		i += 1
		y += 1
		z -= 1
	c = c[i:]
print(''.join(s))


n = int(input())
m = n // 3
a = list(input().strip())
a = list(map(int, a))
count = [0] * 3
for i in a:
	count[i] += 1
if count[0] > m:
	i = c = 0
	while c <= m:
		if a[i] == 0:
			c += 1
		i += 1
	j = i - 1
	while j < n and count[1] < m:
		if a[j] == 0:
			a[j] = 1
			count[0] -= 1
			count[1] += 1
		j += 1
	if count[0] > m:
		while j < n and count[2] < m:
			if a[j] == 0:
				a[j] = 2
				count[0] -= 1
				count[2] += 1
			j += 1
if count[2] > m:
	i = n - 1
	c = 0
	while c <= m:
		if a[i] == 2:
			c += 1
		i -= 1
	j = i + 1
	while j >= 0 and count[1] < m:
		if a[j] == 2:
			a[j] = 1
			count[2] -= 1
			count[1] += 1
		j -= 1
	if count[2] > m:
		while j >= 0 and count[0] < m:
			if a[j] == 2:
				a[j] = 0
				count[2] -= 1
				count[0] += 1
			j -= 1
if count[1] > m:
	j = c = 0
	while j < n and count[0] < m:
		if a[j] == 1:
			a[j] = 0
			count[1] -= 1
			count[0] += 1
		j += 1
	i = j
	while i < n and c <= m:
		if a[i] == 1:
			c += 1
		i += 1
	j = i - 1
	if count[1] > m:
		while j < n and count[2] < m:
			if a[j] == 1:
				a[j] = 2
				count[1] -= 1
				count[2] += 1
			j += 1
print(''.join(map(str, a)))


(N, S) = (int(input()), list(input()))
MyDict = {'0': 0, '1': 0, '2': 0}
Check = [True] * N
for i in S:
	MyDict[i] += 1
(Index, Equal) = (0, N // 3)
while Index != 3:
	This = 0
	for i in range(N):
		if Check[i]:
			if This == Equal:
				break
			if S[i] == str(Index) and MyDict[str(Index)] > Equal:
				Check[i] = False
				S[i] = str(Index)
				This += 1
			elif MyDict[str(Index)] < Equal and MyDict[S[i]] > Equal:
				Check[i] = False
				MyDict[S[i]] -= 1
				MyDict[str(Index)] += 1
				This += 1
				S[i] = str(Index)
	Index += 1
print(''.join(S))


n = int(input())
s = list(input())
zero = []
one = []
two = []
for i in range(n):
	if s[i] == '0':
		zero.append(i)
	if s[i] == '1':
		one.append(i)
	if s[i] == '2':
		two.append(i)
z = len(zero)
o = len(one)
t = len(two)
if z == o and z == t:
	print(''.join(s))
else:
	i = 0
	k = 0
	v = 0
	m = 0
	while k == 0 and i < n:
		if z == o and z == t:
			break
		elif s[i] == '0':
			if z > n // 3 and t < n // 3:
				s[zero[-1]] = '2'
				t += 1
				z -= 1
				zero.pop()
			if z > n // 3 and o < n // 3 and (t >= n // 3):
				s[zero[-1]] = '1'
				o += 1
				z -= 1
				zero.pop()
		elif s[i] == '1':
			if o > n // 3 and z < n // 3:
				s[one[m]] = '0'
				z += 1
				o -= 1
				m += 1
			if o > n // 3 and z >= n // 3 and (t < n // 3):
				s[one[-1]] = '2'
				t += 1
				o -= 1
				one.pop()
		else:
			if t > n // 3 and z < n // 3:
				s[two[v]] = '0'
				t -= 1
				z += 1
				v += 1
			if t > n // 3 and z >= n // 3 and (o < n // 3):
				s[two[v]] = '1'
				o += 1
				t -= 1
				v += 1
		i += 1
	i = 0
	k = 0
	v = 0
	m = 0
	while k == 0 and i < n:
		if z == o and z == t:
			break
		elif s[i] == '0':
			if z > n // 3 and t < n // 3:
				s[zero[-1]] = '2'
				t += 1
				z -= 1
				zero.pop()
			if z > n // 3 and o < n // 3 and (t >= n // 3):
				s[zero[-1]] = '1'
				o += 1
				z -= 1
				zero.pop()
		elif s[i] == '1':
			if o > n // 3 and z < n // 3:
				s[one[m]] = '0'
				z += 1
				o -= 1
				m += 1
			if o > n // 3 and z >= n // 3 and (t < n // 3):
				s[one[-1]] = '2'
				t += 1
				o -= 1
				one.pop()
		else:
			if t > n // 3 and z < n // 3:
				s[two[v]] = '0'
				t -= 1
				z += 1
				v += 1
			if t > n // 3 and z >= n // 3 and (o < n // 3):
				s[two[v]] = '1'
				o += 1
				t -= 1
				v += 1
		i += 1
	print(''.join(s))


import sys
r = lambda : sys.stdin.readline().strip()
n = int(r())
s = r()
a = [0] * 3
for i in s:
	a[ord(i) - ord('0')] += 1
b = [0] * 3
for i in range(3):
	b[i] = a[i] - n // 3
s = list(s)
for i in range(3):
	if b[i] > 0:
		if i == 0:
			tl = [2, 1]
		elif i == 1:
			tl = [0, 2]
		else:
			tl = [0, 1]
		for j in tl:
			if j == i:
				continue
			if b[j] < 0:
				t = min(0 - b[j], b[i])
				b[j] += t
				b[i] -= t
				if j > i:
					for k in range(n - 1, -1, -1):
						if t == 0:
							break
						if s[k] == chr(ord('0') + i):
							s[k] = chr(ord('0') + j)
							t -= 1
				else:
					for k in range(n):
						if t == 0:
							break
						if s[k] == chr(ord('0') + i):
							s[k] = chr(ord('0') + j)
							t -= 1
for i in s:
	print(i, end='')


import re, math, decimal, bisect, collections

def read():
	return input().strip()

def iread():
	return int(input().strip())

def viread():
	return [int(_) for _ in input().strip().split()]
n = iread()
s = list(read())
c = collections.Counter(s)
goal = n // 3
prec = {'0': ['1', '2'], '1': ['0', '2'], '2': ['0', '1']}
seen = collections.defaultdict(int)
for (i, d) in enumerate(s[:]):
	seen[d] += 1
	if c[d] <= goal:
		continue
	elif c[prec[d][0]] < goal:
		if prec[d][0] > d and seen[d] <= goal:
			continue
		s[i] = prec[d][0]
		c[prec[d][0]] += 1
	elif c[prec[d][1]] < goal:
		if prec[d][1] > d and seen[d] <= goal:
			continue
		s[i] = prec[d][1]
		c[prec[d][1]] += 1
	seen[d] -= 1
	c[d] -= 1
print(*s, sep='')


n = int(input())
aa = input()
a = [0] * len(aa)
for i in range(0, len(a)):
	a[i] = int(aa[i])
zero = a.count(0)
one = a.count(1)
two = a.count(2)
i1 = 0
while zero < n // 3:
	if a[i1] == 1 and one > n // 3:
		a[i1] = 0
		zero += 1
		one -= 1
	elif a[i1] == 2 and two > n // 3:
		a[i1] = 0
		zero += 1
		two -= 1
	i1 += 1
i = n - 1
while two < n // 3:
	if a[i] == 1 and one > n // 3:
		a[i] = 2
		two += 1
		one -= 1
	if a[i] == 0 and zero > n // 3:
		a[i] = 2
		zero -= 1
		two += 1
	i -= 1
i = 0
while one < n // 3 and two > n // 3:
	if a[i] == 2:
		a[i] = 1
		two -= 1
		one += 1
	i += 1
i = n - 1
while one < n // 3 and zero > n // 3:
	if a[i] == 0:
		a[i] = 1
		zero -= 1
		one += 1
	i -= 1
for i in range(0, n):
	print(a[i], end='')


from bisect import bisect_left as bl, bisect_right as br, insort
import sys
import heapq
from collections import defaultdict as dd, deque

def data():
	return sys.stdin.readline().strip()

def mdata():
	return map(int, data().split())
mod = 998244353
n = int(data())
s = data()
s1 = list(s)
d = {'0': 0, '1': 0, '2': 0}
for i in range(n):
	d[s[i]] += 1
t = n // 3
if d['0'] < t:
	k = '0'
for i in range(n):
	if d[s1[i]] > t:
		for j in sorted(d.keys()):
			if d[j] < t and j < s1[i]:
				d[j] += 1
				d[s1[i]] -= 1
				s1[i] = j
				break
for i in range(n - 1, -1, -1):
	if d[s1[i]] > t:
		for j in sorted(d.keys(), reverse=True):
			if d[j] < t and j > s1[i]:
				d[j] += 1
				d[s1[i]] -= 1
				s1[i] = j
				break
print(''.join(s1))


n = int(input())
L = [i for i in input()]
p = n // 3
(c0, c1, c2) = (L.count('0'), L.count('1'), L.count('2'))
if c0 == c1 and c1 == c2:
	print(''.join(L))
else:
	s = []
	for i in L:
		if i == '0':
			s.append('0')
		elif i == '1':
			if c0 < p and c1 > p:
				s.append('0')
				c0 += 1
				c1 -= 1
			else:
				s.append('1')
		elif c0 < p and c2 > p:
			s.append('0')
			c0 += 1
			c2 -= 1
		elif c1 < p and c2 > p:
			s.append('1')
			c1 += 1
			c2 -= 1
		else:
			s.append('2')
	for i in reversed(range(n)):
		if L[i] == '0':
			if c0 > p and c2 < p:
				s[i] = '2'
				c0 -= 1
				c2 += 1
			elif c0 > p and c1 < p:
				s[i] = '1'
				c1 += 1
				c0 -= 1
		elif L[i] == '1':
			if c1 > p and c2 < p:
				s[i] = '2'
				c2 += 1
				c1 -= 1
	print(''.join(s))


n = int(input())
arr = [0, 0, 0]
s = input()
for i in range(n):
	arr[int(s[i])] += 1
k = n // 3
res = ''
re = [0, 0, 0]
for i in range(n):
	m = int(s[i])
	if arr[m] > k:
		if m == 0:
			if re[0] < k:
				res += '0'
				re[0] += 1
				continue
			else:
				arr[0] -= 1
				if arr[1] < k and re[1] < k:
					res += '1'
					re[1] += 1
					arr[1] += 1
					continue
				else:
					res += '2'
					re[2] += 1
					arr[2] += 1
					continue
		if m == 1:
			if arr[0] < k:
				res += '0'
				arr[0] += 1
				arr[1] -= 1
				continue
			if re[1] < k:
				re[1] += 1
				res += '1'
				continue
			res += '2'
			arr[2] += 1
			re[2] += 1
			continue
		if m == 2:
			arr[2] -= 1
			if re[0] < k and arr[0] < k:
				res += '0'
				arr[0] += 1
				re[0] += 1
				continue
			res += '1'
			arr[1] += 1
			re[1] += 1
	else:
		res += s[i]
		re[m] += 1
print(res)


def opt(n1, n2, n3, n):
	v = n // 3
	return max(abs(n1 - v), abs(n2 - v), abs(n3 - v))
n = int(input())
d = str(input())
s = [d[i] for i in range(n)]
n1 = s.count('0')
n2 = s.count('1')
n3 = s.count('2')
for i in range(0, len(s)):
	a = s[i]
	if a == '1':
		if opt(n1 + 1, n2 - 1, n3, n) == opt(n1, n2, n3, n) - 1:
			s[i] = '0'
			n1 += 1
			n2 -= 1
	elif a == '2':
		if opt(n1 + 1, n2, n3 - 1, n) == opt(n1, n2, n3, n) - 1:
			s[i] = '0'
			n1 += 1
			n3 -= 1
		elif opt(n1, n2 + 1, n3 - 1, n) == opt(n1, n2, n3, n) - 1:
			s[i] = '1'
			n2 += 1
			n3 -= 1
u = n // 3
v1 = n1 - u
v2 = n2 - u
v3 = n3 - u
if v2 >= 0:
	for i in range(len(s) - 1, -1, -1):
		if s[i] == '1':
			if v2 > 0:
				s[i] = '2'
				v2 -= 1
				v3 += 1
		elif s[i] == '0':
			if v1 > 0:
				s[i] = '2'
				v1 -= 1
				v3 += 1
else:
	for i in range(len(s) - 1, -1, -1):
		if s[i] == '0':
			if v3 < 0:
				s[i] = '2'
				v1 -= 1
				v3 += 1
			elif v2 < 0:
				s[i] = '1'
				v1 -= 1
				v2 += 1
for i in s:
	print(i, end='')


def toosmall0(loc0, loc1, loc2, opt):
	if len(loc2) > opt:
		loc2.reverse()
		while len(loc2) > opt and len(loc0) < opt:
			new = loc2.pop()
			loc0.append(new)
		loc0.sort()
		loc2.reverse()
	if len(loc1) > opt:
		loc1.reverse()
		while len(loc1) > opt and len(loc0) < opt:
			new = loc1.pop()
			loc0.append(new)
		loc0.sort()
		loc1.reverse()
	return (loc0, loc1, loc2)

def toobig0(loc0, loc1, loc2, opt):
	if len(loc2) < opt:
		while len(loc2) < opt and len(loc0) > opt:
			new = loc0.pop()
			loc2.append(new)
		loc0.sort()
	if len(loc1) < opt:
		while len(loc1) < opt and len(loc0) > opt:
			new = loc0.pop()
			loc1.append(new)
		loc0.sort()
	return (loc0, loc1, loc2)

def f(s):
	opt = int(len(s) / 3)
	loc0 = []
	loc1 = []
	loc2 = []
	for (i, char) in enumerate(s):
		if char == '0':
			loc0.append(i)
		elif char == '1':
			loc1.append(i)
		else:
			loc2.append(i)
	if len(loc0) < opt:
		(loc0, loc1, loc2) = toosmall0(loc0, loc1, loc2, opt)
	elif len(loc0) > opt:
		(loc0, loc1, loc2) = toobig0(loc0, loc1, loc2, opt)
	if len(loc2) < opt and len(loc1) > opt:
		while len(loc2) < opt and len(loc1) > opt:
			new = loc1.pop()
			loc2.append(new)
		loc2.sort()
	if len(loc2) > opt and len(loc1) < opt:
		loc2.reverse()
		while len(loc2) > opt and len(loc1) < opt:
			new = loc2.pop()
			loc1.append(new)
	output = [('0', el) for el in loc0] + [('1', el) for el in loc1] + [('2', el) for el in loc2]
	output.sort(key=lambda x: x[1])
	output = [el[0] for el in output]
	return ''.join(output)
n = int(input())
s = input()
output = f(s)
print(output)


n = int(input())
a = input()
(cnt0, cnt1, cnt2) = (a.count('0') - n // 3, a.count('1') - n // 3, a.count('2') - n // 3)
occ = [0, 0, 0]
for i in range(n):
	if a[i] == '0':
		if cnt0 > 0 and occ[int(a[i])] >= n // 3:
			if cnt1 < 0:
				print(1, end='')
				cnt1 += 1
			else:
				print(2, end='')
				cnt2 += 1
			cnt0 -= 1
		else:
			occ[int(a[i])] += 1
			print(a[i], end='')
	elif a[i] == '1':
		if cnt1 > 0:
			if cnt0 < 0:
				print(0, end='')
				cnt0 += 1
			elif occ[int(a[i])] >= n // 3:
				print(2, end='')
				cnt2 += 1
			else:
				print(a[i], end='')
				occ[int(a[i])] += 1
				cnt1 += 1
			cnt1 -= 1
		else:
			occ[int(a[i])] += 1
			print(a[i], end='')
	elif cnt2 > 0:
		if cnt0 < 0:
			print(0, end='')
			cnt0 += 1
		else:
			print(1, end='')
			cnt1 += 1
		cnt2 -= 1
	else:
		print(a[i], end='')


import sys
n = int(sys.stdin.readline().strip())
s = sys.stdin.readline().strip()
A = [0] * n
k = n // 3
c0 = 0
c1 = 0
c2 = 0
for i in range(0, n):
	if s[i] == '0':
		c0 = c0 + 1
	elif s[i] == '1':
		c1 = c1 + 1
	else:
		c2 = c2 + 1
	A[i] = s[i]
i = 0
while c0 < k:
	if A[i] == '1' and c1 > k:
		A[i] = '0'
		c0 = c0 + 1
		c1 = c1 - 1
	elif A[i] == '2' and c2 > k:
		A[i] = '0'
		c0 = c0 + 1
		c2 = c2 - 1
	i = i + 1
i = n - 1
while c0 > k and c2 < k:
	if A[i] == '0':
		A[i] = '2'
		c0 = c0 - 1
		c2 = c2 + 1
	i = i - 1
while c0 > k and c1 < k:
	if A[i] == '0':
		A[i] = '1'
		c0 = c0 - 1
		c1 = c1 + 1
	i = i - 1
i = n - 1
while c1 > k and c2 < k:
	if A[i] == '1':
		A[i] = '2'
		c1 = c1 - 1
		c2 = c2 + 1
	i = i - 1
i = 0
while c1 < k and c2 > k:
	if A[i] == '2':
		A[i] = '1'
		c2 = c2 - 1
		c1 = c1 + 1
	i = i + 1
print(''.join(A))


n = int(input())
s = list(input())
a = [0, 0, 0]
for i in s:
	if i == '0':
		a[0] += 1
	elif i == '1':
		a[1] += 1
	else:
		a[2] += 1
k = n // 3
if a[0] == a[1] and a[1] == a[2]:
	print(''.join(s))
	exit(0)
x = a[0]
y = a[1]
z = a[2]
if x < k:
	for i in range(n):
		if x == k:
			break
		if z > k and s[i] == '2':
			s[i] = '0'
			z -= 1
			x += 1
		elif y > k and s[i] == '1':
			s[i] = '0'
			y -= 1
			x += 1
if z < k:
	for i in range(n - 1, -1, -1):
		if z == k:
			break
		if x > k and s[i] == '0':
			s[i] = '2'
			x -= 1
			z += 1
		if y > k and s[i] == '1':
			s[i] = '2'
			y -= 1
			z += 1
if y < k:
	for i in range(n - 1, -1, -1):
		if y == k:
			break
		if x > k and s[i] == '0':
			s[i] = '1'
			x -= 1
			y += 1
	for i in range(n):
		if y == k:
			break
		if z > k and s[i] == '2':
			s[i] = '1'
			z -= 1
			y += 1
print(''.join(s))


rd = lambda : list(map(int, input().split()))
(n, s) = (rd()[0], [ord(c) - ord('0') for c in input()])
k = n // 3
(precnt, cnt) = ([0] * 3, [0] * 3)
for x in s:
	precnt[x] += 1
ans = []
for x in s:
	fl = True
	if x == 0:
		if cnt[0] >= k:
			precnt[0] -= 1
			if precnt[1] < k:
				x = 1
				precnt[1] += 1
			else:
				x = 2
				precnt[2] += 1
	elif x == 1:
		if precnt[1] > k:
			if precnt[0] < k:
				precnt[1] -= 1
				x = 0
				precnt[0] += 1
				fl = False
		if fl and cnt[1] >= k:
			precnt[1] -= 1
			if precnt[0] < k:
				x = 0
				precnt[0] += 1
			else:
				x = 2
				precnt[2] += 1
	else:
		if precnt[2] > k:
			if precnt[0] < k:
				precnt[2] -= 1
				x = 0
				precnt[0] += 1
				fl = False
			elif precnt[1] < k:
				precnt[2] -= 1
				x = 1
				precnt[1] += 1
				fl = False
		if fl and cnt[2] >= k:
			precnt[2] -= 1
			if precnt[0] < k:
				x = 0
				precnt[0] += 1
			else:
				x = 1
				precnt[1] += 1
	cnt[x] += 1
	ans.append(x)
print(''.join([str(x) for x in ans]))


n = int(input()) // 3
s = input()
a = [n - s.count(str(i)) for i in range(3)]
for i in [0, 2, 1]:
	if a[i] > 0:
		for j in range(3):
			if a[j] < 0:
				r = min(a[i], abs(a[j]))
				a[i] -= r
				a[j] += r
				if i > j:
					s = s[::-1].replace(str(j), str(i), r)[::-1]
				else:
					s = s.replace(str(j), str(i), r)
print(s)


n = int(input())
s = input()
a = []
for i in s:
	a.append(int(i))
c = [0] * 3
id = [0] * 3
l = [0] * 3
r = [0] * 3
for i in range(3):
	id[i] = []
d = 0
for i in a:
	id[i].append(d)
	c[i] += 1
	d += 1
for i in range(3):
	r[i] = len(id[i]) - 1
lel = n // 3

def transfer(pos, x, y):
	a[pos] = x
	c[x] += 1
	c[y] -= 1

def empty(x):
	return l[x] > r[x]

def do0():
	while c[0] < lel:
		pos1 = n
		pos2 = n
		if not empty(2) and c[2] > lel:
			pos2 = id[2][l[2]]
		if not empty(1) and c[1] > lel:
			pos1 = id[1][l[1]]
		if pos2 <= pos1:
			transfer(pos2, 0, 2)
			l[2] += 1
		else:
			transfer(pos1, 0, 1)
			l[1] += 1

def do1():
	while c[1] < lel:
		if not empty(2) and c[2] > lel:
			pos = id[2][l[2]]
			transfer(pos, 1, 2)
			l[2] += 1
		elif not empty(0) and c[0] > lel:
			pos = id[0][r[0]]
			transfer(pos, 1, 0)
			r[0] -= 1

def do2():
	while c[2] < lel:
		pos1 = 0
		pos0 = 0
		if not empty(1) and c[1] > lel:
			pos1 = id[1][r[1]]
		if not empty(0) and c[0] > lel:
			pos0 = id[0][r[0]]
		if pos1 >= pos0:
			transfer(pos1, 2, 1)
			r[1] -= 1
		else:
			transfer(pos0, 2, 0)
			r[0] -= 1
if c[0] >= c[1] and c[0] >= c[2]:
	do2()
	do1()
elif c[1] >= c[0] and c[1] >= c[2]:
	do0()
	do2()
elif c[2] >= c[1] and c[2] >= c[0]:
	do0()
	do1()
for i in a:
	print(i, end='')


def D():
	n = int(input())
	s = input()
	c = [0, 0, 0]
	for d in s:
		if d == '0':
			c[0] += 1
		elif d == '1':
			c[1] += 1
		else:
			c[2] += 1
	a = [x for x in s]
	for i in range(n):
		act = s[i]
		if c[int(act)] > n // 3:
			for j in range(int(act)):
				if c[j] < n // 3:
					c[j] += 1
					c[int(a[i])] -= 1
					a[i] = str(j)
					break
	for i in range(n - 1, -1, -1):
		act = s[i]
		if c[int(act)] > n // 3:
			for j in range(2, -1, -1):
				if c[j] < n // 3:
					c[j] += 1
					c[int(s[i])] -= 1
					a[i] = str(j)
					break
	print(''.join(a))
D()


n = int(input())
s = input()
c1 = 0
c2 = 0
c3 = 0
for i in range(n):
	if s[i] == '0':
		c1 += 1
	elif s[i] == '1':
		c2 += 1
	elif s[i] == '2':
		c3 += 1
st = []
for i in range(n):
	st.append(s[i])
if c1 == c2 == c3:
	print(s)
elif c1 == n // 3 and c2 < c3:
	for i in range(n):
		if st[i] == '2' and c2 < c3:
			st[i] = 1
			c2 += 1
			c3 -= 1
	for i in range(n):
		print(st[i], end='')
	print()
elif c1 == n // 3 and c3 < c2:
	for i in range(n - 1, -1, -1):
		if st[i] == '1' and c3 < c2:
			st[i] = 2
			c3 += 1
			c2 -= 1
	for i in range(n):
		print(st[i], end='')
	print()
elif c2 == n // 3 and c1 < c3:
	for i in range(n):
		if st[i] == '2' and c1 < c3:
			st[i] = 0
			c1 += 1
			c3 -= 1
	for i in range(n):
		print(st[i], end='')
	print()
elif c2 == n // 3 and c3 < c1:
	for i in range(n - 1, -1, -1):
		if st[i] == '0' and c3 < c1:
			st[i] = 2
			c3 += 1
			c1 -= 1
	for i in range(n):
		print(st[i], end='')
	print()
elif c3 == n // 3 and c1 < c2:
	for i in range(n):
		if st[i] == '1' and c1 < c2:
			st[i] = 0
			c1 += 1
			c2 -= 1
	for i in range(n):
		print(st[i], end='')
	print()
elif c3 == n // 3 and c2 < c1:
	for i in range(n - 1, -1, -1):
		if st[i] == '0' and c2 < c1:
			st[i] = 1
			c2 += 1
			c1 -= 1
	for i in range(n):
		print(st[i], end='')
	print()
elif c3 > n // 3 and c2 < n // 3 and (c1 < n // 3):
	for i in range(n):
		if st[i] == '2' and c1 < n // 3:
			st[i] = 0
			c1 += 1
			c3 -= 1
	for i in range(n):
		if st[i] == '2' and c2 < n // 3:
			st[i] = 1
			c2 += 1
			c3 -= 1
	for i in range(n):
		print(st[i], end='')
	print()
elif c2 > n // 3 and c3 < n // 3 and (c1 < n // 3):
	for i in range(n):
		if st[i] == '1' and c1 < n // 3:
			st[i] = 0
			c1 += 1
			c2 -= 1
	for i in range(n - 1, -1, -1):
		if st[i] == '1' and c3 < n // 3:
			st[i] = 2
			c3 += 1
			c2 -= 1
	for i in range(n):
		print(st[i], end='')
	print()
elif c1 > n // 3 and c2 < n // 3 and (c3 < n // 3):
	for i in range(n - 1, -1, -1):
		if st[i] == '0' and c3 < n // 3:
			st[i] = 2
			c3 += 1
			c1 -= 1
	for i in range(n - 1, -1, -1):
		if st[i] == '0' and c2 < n // 3:
			st[i] = 1
			c2 += 1
			c1 -= 1
	for i in range(n):
		print(st[i], end='')
	print()
elif c3 > n // 3 and c2 > n // 3 and (c1 < n // 3):
	for i in range(n):
		if st[i] == '2' and c3 > n // 3:
			st[i] = 0
			c1 += 1
			c3 -= 1
		elif st[i] == '1' and c2 > n // 3:
			st[i] = 0
			c1 += 1
			c2 -= 1
	for i in range(n):
		print(st[i], end='')
	print()
elif c1 > n // 3 and c2 > n // 3 and (c3 < n // 3):
	for i in range(n - 1, -1, -1):
		if st[i] == '0' and c1 > n // 3:
			st[i] = 2
			c1 -= 1
			c3 += 1
		elif st[i] == '1' and c2 > n // 3:
			st[i] = 2
			c2 -= 1
			c3 += 1
	for i in range(n):
		print(st[i], end='')
	print()
elif c1 > n // 3 and c3 > n // 3 and (c2 < n // 3):
	for i in range(n):
		if st[i] == '2' and c3 > n // 3:
			st[i] = 1
			c3 -= 1
			c2 += 1
	for i in range(n - 1, -1, -1):
		if st[i] == '0' and c1 > n // 3:
			st[i] = 1
			c1 -= 1
			c2 += 1
	for i in range(n):
		print(st[i], end='')
	print()


n = int(input())
s = input()
counters = {'0': 0, '1': 0, '2': 0}
each = int(n / 3)

def king():
	for char in s:
		counters[char] += 1
	for key in counters:
		counters[key] -= each
king()
backward = '210'
for back in backward:
	if counters[back] > 0:
		for (num, counts) in counters.items():
			if counts < 0 and int(num) < int(back):
				changes = min(abs(counts), counters[back])
				s = s.replace(back, num, changes)
				counters[back] -= changes
				counters[num] += changes
s = s[::-1]
for back in backward:
	if counters[back] < 0:
		for (num, counts) in counters.items():
			if counts > 0 and int(back) > int(num):
				changes = min(counts, abs(counters[back]))
				s = s.replace(num, back, changes)
				counters[back] -= changes
				counters[num] += changes
s = s[::-1]
print(s)


from collections import defaultdict
n = int(input())
s = input()
l = [i for i in s]
freq = defaultdict()
equal = n // 3
freq['0'] = s.count('0')
freq['1'] = s.count('1')
freq['2'] = s.count('2')
for i in range(n):
	curr = l[i]
	if freq['0'] < equal:
		if freq[curr] > equal:
			l[i] = '0'
			freq[curr] -= 1
			freq['0'] += 1
for i in reversed(range(n)):
	curr = l[i]
	if freq['2'] < equal:
		if freq[curr] > equal:
			l[i] = '2'
			freq[curr] -= 1
			freq['2'] += 1
for i in range(n):
	curr = l[i]
	if freq['1'] < equal:
		if freq['2'] > equal:
			if curr == '2':
				l[i] = '1'
				freq[curr] -= 1
				freq['1'] += 1
for i in reversed(range(n)):
	curr = l[i]
	if freq['1'] < equal:
		if freq['0'] > equal:
			if curr == '0':
				l[i] = '1'
				freq[curr] -= 1
				freq['1'] += 1
print(''.join(map(str, l)))


from collections import defaultdict, deque, Counter
from sys import stdin, stdout
from heapq import heappush, heappop
import math
import io
import os
import math
import bisect

def isPrime(x):
	for i in range(2, x):
		if i * i > x:
			break
		if x % i == 0:
			return False
	return True

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(int(i))
			n = n / i
	if n > 2:
		l.append(n)
	return list(set(l))

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def digits(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def ceil(n, x):
	if n % x == 0:
		return n // x
	return n // x + 1

def mapin():
	return map(int, input().split())
n = int(input())
l = list(input())
d = n // 3
dd = [0, 0, 0]
for i in range(n):
	dd[int(l[i])] += 1
for i in range(n):
	if dd[0] < d and int(l[i]) > 0 and (dd[int(l[i])] > d):
		dd[0] += 1
		dd[int(l[i])] -= 1
		l[i] = '0'
for i in range(n):
	if dd[1] < d and int(l[i]) == 2 and (dd[2] > d):
		l[i] = '1'
		dd[1] += 1
		dd[2] -= 1
for ii in range(n):
	i = n - 1 - ii
	if dd[2] < d and int(l[i]) < 2 and (dd[int(l[i])] > d):
		dd[int(l[i])] -= 1
		l[i] = '2'
		dd[2] += 1
for ii in range(n):
	i = n - 1 - ii
	if dd[1] < d and int(l[i]) == 0 and (dd[int(l[i])] > d):
		dd[0] -= 1
		l[i] = '1'
		dd[1] += 1
print(''.join(l))


n = int(input())
bal = {'0': 0, '1': 0, '2': 0}
s = input()
for el in s:
	bal[el] += 1
for i in range(3):
	bal[str(i)] -= n // 3
s = list(s)
for i in range(n):
	if bal[s[i]] > 0:
		for j in range(int(s[i])):
			if bal[str(j)] < 0:
				bal[s[i]] -= 1
				bal[str(j)] += 1
				s[i] = str(j)
				break
for to in [2, 1]:
	for frm in [0, 1, 2]:
		for i in range(n - 1, -1, -1):
			if s[i] == str(frm) and bal[str(frm)] > 0:
				if bal[str(to)] < 0:
					bal[str(frm)] -= 1
					bal[str(to)] += 1
					s[i] = str(to)
print(''.join(s))


from __future__ import division, print_function
from sys import stdin, stdout
import bisect
import math
import heapq

def modinv(n, p):
	return pow(n, p - 2, p)

def cin():
	return map(int, sin().split())

def ain():
	return list(map(int, sin().split()))

def sin():
	return input()

def inin():
	return int(input())
import math

def Divisors(n):
	l = []
	for i in range(1, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n // i == i:
				l.append(i)
			else:
				l.append(i)
				l.append(n // i)
	return l
q = []

def dfs(n, d, v, c):
	global q
	v[n] = 1
	x = d[n]
	q.append(n)
	j = c
	for i in x:
		if i not in v:
			f = dfs(i, d, v, c + 1)
			j = max(j, f)
	return j

def main():
	n = inin()
	s = sin()
	x = s.count('0')
	y = s.count('1')
	z = s.count('2')
	r = n // 3
	s = list(s)
	for i in range(0, n):
		if x < r:
			if s[i] == '1' and y > r:
				s[i] = '0'
				x += 1
				y -= 1
			if s[i] == '2' and z > r:
				s[i] = '0'
				x += 1
				z -= 1
	for i in range(n - 1, -1, -1):
		if z < r:
			if s[i] == '1' and y > r:
				s[i] = '2'
				z += 1
				y -= 1
			if s[i] == '0' and x > r:
				s[i] = '2'
				z += 1
				x -= 1
	for i in range(n - 1, -1, -1):
		if y < r:
			if s[i] == '0' and x > r:
				s[i] = '1'
				x -= 1
				y += 1
	for i in range(0, n):
		if y < r:
			if s[i] == '2' and z > r:
				s[i] = '1'
				y += 1
				z -= 1
	print(*s, sep='')
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import sys

class ostream:

	def __lshift__(self, a):
		sys.stdout.write(str(a))
		return self
cout = ostream()
endl = '\n'

def readnumbers(zero=0):
	conv = ord if py2 else lambda x: x
	A = []
	numb = zero
	sign = 1
	i = 0
	s = sys.stdin.buffer.read()
	try:
		while True:
			if s[i] >= b'0'[0]:
				numb = 10 * numb + conv(s[i]) - 48
			elif s[i] == b'-'[0]:
				sign = -1
			elif s[i] != b'\r'[0]:
				A.append(sign * numb)
				numb = zero
				sign = 1
			i += 1
	except:
		pass
	if s and s[-1] >= b'0'[0]:
		A.append(sign * numb)
	return A
main()


from sys import stdin, stdout
from math import sin, tan, cos, pi, atan2, sqrt, acos, atan, factorial
from random import randint
n = int(stdin.readline())
s = list(stdin.readline().strip())
(a, b, c) = (s.count('0'), s.count('1'), s.count('2'))
d = n // 3
for i in range(len(s)):
	if s[i] == '2' and c > d:
		if a < d:
			s[i] = '0'
			a += 1
			c -= 1
		else:
			s[i] = '1'
			b += 1
			c -= 1
	elif s[i] == '1' and b > d:
		if a < d:
			s[i] = '0'
			a += 1
			b -= 1
for i in range(len(s) - 1, -1, -1):
	if s[i] == '1' and b > d:
		if c < d:
			s[i] = '2'
			b -= 1
			c += 1
	elif s[i] == '0' and a > d:
		if c < d:
			s[i] = '2'
			a -= 1
			c += 1
		elif b < d:
			s[i] = '1'
			a -= 1
			b += 1
stdout.write(''.join(s))


n = int(input())
s = list(input())
zero = s.count('0')
one = s.count('1')
two = s.count('2')
n1 = n // 3
if two < n1 and zero > n1:
	c = n - 1
	while two != n1 and zero != n1:
		if s[c] == '0':
			s[c] = '2'
			two += 1
			zero -= 1
		c -= 1
if two > n1 and zero < n1:
	c = 0
	while two != n1 and zero != n1:
		if s[c] == '2':
			s[c] = '0'
			two -= 1
			zero += 1
		c += 1
if two > n1 and one < n1:
	c = 0
	while two != n1 and one != n1:
		if s[c] == '2':
			s[c] = '1'
			two -= 1
			one += 1
		c += 1
if two < n1 and one > n1:
	c = n - 1
	while two != n1 and one != n1:
		if s[c] == '1':
			s[c] = '2'
			two += 1
			one -= 1
		c -= 1
if one > n1 and zero < n1:
	c = 0
	while one != n1 and zero != n1:
		if s[c] == '1':
			s[c] = '0'
			one -= 1
			zero += 1
		c += 1
if one < n1 and zero > n1:
	c = n - 1
	while one != n1 and zero != n1:
		if s[c] == '0':
			s[c] = '1'
			one += 1
			zero -= 1
		c -= 1
print(''.join(s))


import sys
LI = lambda : list(map(int, sys.stdin.readline().split()))
MI = lambda : map(int, sys.stdin.readline().split())
SI = lambda : sys.stdin.readline().strip('\n')
II = lambda : int(sys.stdin.readline())
n = II() // 3
s = list(SI())
cnt = {x: s.count(x) for x in '012'}

def frw(x):
	for i in range(n * 3):
		if cnt[x] < n and s[i] > x and (cnt[s[i]] > n):
			cnt[x] += 1
			cnt[s[i]] -= 1
			s[i] = x

def bck(x):
	for i in range(n * 3 - 1, -1, -1):
		if cnt[x] < n and s[i] < x and (cnt[s[i]] > n):
			cnt[x] += 1
			cnt[s[i]] -= 1
			s[i] = x
frw('0')
frw('1')
bck('2')
bck('1')
print(''.join(s))


n = int(input())
s = list(input())
sum0 = 0
sum1 = 0
for char in s:
	if char == '0':
		sum0 += 1
	elif char == '1':
		sum1 += 1
sum2 = n - sum0 - sum1
passed0 = 0
passed1 = 0
for (i, char) in enumerate(s):
	if char == '0':
		if sum0 > n / 3:
			if passed0 >= n / 3:
				if sum1 < n / 3:
					s[i] = '1'
					sum0 -= 1
					sum1 += 1
				elif sum2 < n / 3:
					s[i] = '2'
					sum0 -= 1
					sum2 += 1
			else:
				passed0 += 1
	elif char == '1':
		if sum1 > n / 3:
			if sum0 < n / 3:
				s[i] = '0'
				sum0 += 1
				sum1 -= 1
			elif passed1 >= n / 3:
				if sum2 < n / 3:
					s[i] = '2'
					sum1 -= 1
					sum2 += 1
			else:
				passed1 += 1
	elif char == '2':
		if sum2 > n / 3:
			if sum0 < n / 3:
				s[i] = '0'
				sum0 += 1
				sum2 -= 1
			elif sum1 < n / 3:
				s[i] = '1'
				sum1 += 1
				sum2 -= 1
print(''.join(s))


N = int(input())
s = input()
ans = list(s)
freq = [0, 0, 0]
for letter in s:
	if letter == '0':
		freq[0] += 1
	if letter == '1':
		freq[1] += 1
	if letter == '2':
		freq[2] += 1
base = N // 3
i = 0
for j in range(2, 0, -1):
	if i != j:
		if freq[i] > base and freq[j] < base:
			fix = min(freq[i] - base, base - freq[j])
			freq[i] -= fix
			freq[j] += fix
			fixed = 0
			for k in range(N - 1, -1, -1):
				if ans[k] == str(i):
					ans[k] = str(j)
					fixed += 1
				if fixed == fix:
					break
i = 1
j = 2
if i != j:
	if freq[i] > base and freq[j] < base:
		fix = min(freq[i] - base, base - freq[j])
		freq[i] -= fix
		freq[j] += fix
		fixed = 0
		for k in range(N - 1, -1, -1):
			if ans[k] == str(i):
				ans[k] = str(j)
				fixed += 1
			if fixed == fix:
				break
i = 1
j = 0
if i != j:
	if freq[i] > base and freq[j] < base:
		fix = min(freq[i] - base, base - freq[j])
		freq[i] -= fix
		freq[j] += fix
		fixed = 0
		for k in range(N):
			if ans[k] == str(i):
				ans[k] = str(j)
				fixed += 1
			if fixed == fix:
				break
i = 2
for j in range(2):
	if i != j:
		if freq[i] > base and freq[j] < base:
			fix = min(freq[i] - base, base - freq[j])
			freq[i] -= fix
			freq[j] += fix
			fixed = 0
			for k in range(N):
				if ans[k] == str(i):
					ans[k] = str(j)
					fixed += 1
				if fixed == fix:
					break
for letter in ans:
	print(letter, end='')


import sys
from math import ceil
read = sys.stdin.buffer.readline
mi = lambda : map(int, read().split())
li = lambda : list(mi())
cin = lambda : int(read())
d = {'RR': 'G', 'RG': 'B', 'GR': 'B', 'RB': 'G', 'BR': 'G', 'GG': 'R', 'GB': 'R', 'BG': 'R', 'BB': 'R'}
for _ in range(1):
	n = cin()
	s = list(input())
	d = {'0': 0, '1': 0, '2': 0}
	for el in s:
		d[el] += 1
	if d['0'] < n // 3:
		req = n // 3 - d['0']
		i = 0
		while d['2'] > n // 3 and req > 0:
			if s[i] == '2':
				s[i] = '0'
				req -= 1
				d['2'] -= 1
			i += 1
		i = 0
		while d['1'] > n // 3 and req > 0:
			if s[i] == '1':
				s[i] = '0'
				req -= 1
				d['1'] -= 1
			i += 1
	if d['1'] < n // 3:
		req = n // 3 - d['1']
		i = 0
		while d['2'] > n // 3 and req > 0:
			if s[i] == '2':
				s[i] = '1'
				req -= 1
				d['2'] -= 1
				d['1'] += 1
			i += 1
		(ind, rr) = (0, 0)
		for i in range(n):
			if s[i] == '0':
				rr += 1
			if rr == n // 3:
				ind = i + 1
				break
		i = ind
		while d['0'] > n // 3 and req > 0:
			if s[i] == '0':
				s[i] = '1'
				req -= 1
				d['0'] -= 1
				d['1'] += 1
			i += 1
	if d['2'] < n // 3:
		req = n // 3 - d['2']
		i = n - 1
		while d['1'] > n // 3 and req > 0:
			if s[i] == '1':
				s[i] = '2'
				req -= 1
				d['1'] -= 1
			i -= 1
		i = n - 1
		while d['0'] > n // 3 and req > 0:
			if s[i] == '0':
				s[i] = '2'
				req -= 1
				d['0'] -= 1
			i -= 1
	for el in s:
		print(el, end='')


n = int(input())
s = input()
a = [int(i) for i in s]
b = [0] * 3
size = n // 3
for i in range(len(a)):
	b[int(a[i])] += 1
if b[0] == b[1] == b[2]:
	print(s)
else:
	if b[0] >= b[1] and b[0] >= b[2]:
		for i in range(len(a) - 1, -1, -1):
			if b[2] < size and b[a[i]] > size:
				b[a[i]] -= 1
				a[i] = 2
				b[2] += 1
		for i in range(len(a)):
			if b[1] < size and b[a[i]] > size and (a[i] == 2):
				b[a[i]] -= 1
				a[i] = 1
				b[1] += 1
		for i in range(len(a) - 1, -1, -1):
			if b[1] < size and b[a[i]] > size and (a[i] == 0):
				b[a[i]] -= 1
				a[i] = 1
				b[1] += 1
	elif b[1] >= b[0] and b[1] >= b[2]:
		for i in range(len(a)):
			if b[0] < size and b[a[i]] > size:
				b[a[i]] -= 1
				a[i] = 0
				b[0] += 1
		for i in range(len(a) - 1, -1, -1):
			if b[2] < size and b[a[i]] > size:
				b[a[i]] -= 1
				a[i] = 2
				b[2] += 1
	else:
		for i in range(len(a)):
			if b[0] < size and b[a[i]] > size:
				b[a[i]] -= 1
				a[i] = 0
				b[0] += 1
		for i in range(len(a)):
			if b[1] < size and b[a[i]] > size and (a[i] == 2):
				b[a[i]] -= 1
				a[i] = 1
				b[1] += 1
		for i in range(len(a) - 1, -1, -1):
			if b[1] < size and b[a[i]] > size and (a[i] == 0):
				b[a[i]] -= 1
				a[i] = 1
				b[1] += 1
	s = [str(i) for i in a]
	print(''.join(s))


from collections import Counter
from itertools import takewhile

def main():
	n = int(input())
	s = list(input())
	balance = n / 3
	count = Counter(s)

	def set_char(idx, value):
		count[value] += 1
		count[s[idx]] -= 1
		s[idx] = value
	for i in range(n):
		if balance < count[s[i]]:
			for replacement in '01'[:int(s[i])]:
				if count[replacement] < balance:
					set_char(i, replacement)
					break
	for i in range(n - 1, -1, -1):
		if balance < count[s[i]]:
			for replacement in '21':
				if count[replacement] < balance:
					set_char(i, replacement)
					break
	assert all((cnt == balance for cnt in count.values()))
	print(''.join(s))
main()


n = int(input())
s = list(input())
d = {}
a = n // 3
d['0'] = 0
d['1'] = 0
d['2'] = 0
cur = {}
cur['0'] = 0
cur['1'] = 0
cur['2'] = 0
for i in s:
	if i in d:
		d[i] += 1
for k in range(len(s)):
	i = s[k]
	if d[i] > a:
		for j in d:
			if i != j and d[j] < a:
				if j > i and cur[i] < a:
					cur[i] += 1
					break
				d[i] -= 1
				d[j] += 1
				s[k] = j
				break
print(''.join(s))


from bisect import bisect_right as br
from bisect import bisect_left as bl
from collections import defaultdict
from itertools import combinations
import functools
import sys
import math
MAX = sys.maxsize
MAXN = 10 ** 6 + 10
MOD = 10 ** 9 + 7

def isprime(n):
	n = abs(int(n))
	if n < 2:
		return False
	if n == 2:
		return True
	if not n & 1:
		return False
	for x in range(3, int(n ** 0.5) + 1, 2):
		if n % x == 0:
			return False
	return True

def mhd(a, b, x, y):
	return abs(a - x) + abs(b - y)

def numIN():
	return map(int, sys.stdin.readline().strip().split())

def charIN():
	return sys.stdin.readline().strip().split()

def ncr(n, k):
	res = 1
	c = [0] * (k + 1)
	c[0] = 1
	for i in range(1, n + 1):
		for j in range(min(i, k), 0, -1):
			c[j] = (c[j] + c[j - 1]) % MOD
	return c[k]
n = int(input())
s = [int(i) for i in input()]
cnt = [0] * 3
for i in s:
	cnt[i] += 1
for i in range(n):
	if cnt[s[i]] > n // 3:
		for j in range(s[i]):
			if cnt[j] < n // 3:
				cnt[s[i]] -= 1
				s[i] = j
				cnt[j] += 1
				break
for i in range(n - 1, -1, -1):
	if cnt[s[i]] > n // 3:
		for j in range(2, -1, -1):
			if j != s[i]:
				if cnt[j] < n // 3:
					cnt[s[i]] -= 1
					s[i] = j
					cnt[j] += 1
					break
print(''.join([str(i) for i in s]))


def main():
	(n, s) = (int(input()), input())
	cnt = {c: s.count(c) for c in '012'}
	n //= 3
	l = list(s)
	if cnt['0'] < n:
		for (i, c) in enumerate(l):
			if cnt[c] > n:
				l[i] = '0'
				cnt[c] -= 1
				cnt['0'] += 1
				if cnt['0'] == n:
					break
	if cnt['1'] < n < cnt['2']:
		for (i, c) in enumerate(l):
			if c == '2':
				l[i] = '1'
				cnt['2'] -= 1
				cnt['1'] += 1
				if not cnt['1'] < n < cnt['2']:
					break
	l.reverse()
	if cnt['2'] < n:
		for (i, c) in enumerate(l):
			if cnt[c] > n:
				l[i] = '2'
				cnt[c] -= 1
				cnt['2'] += 1
				if cnt['2'] == n:
					break
	if cnt['1'] < n < cnt['0']:
		for (i, c) in enumerate(l):
			if c == '0':
				l[i] = '1'
				cnt['0'] -= 1
				cnt['1'] += 1
				if not cnt['1'] < n < cnt['0']:
					break
	print(''.join(l[::-1]))
main()


n = int(input())
a = list(map(int, list(input())))
cs = [0, 0, 0]
for i in a:
	cs[i] += 1
ops = 0
for i in cs:
	ops += max(0, i - n // 3)
if cs[0] < n // 3:
	for i in range(n):
		if a[i] != 0 and cs[a[i]] > n // 3:
			cs[a[i]] -= 1
			a[i] = 0
			cs[0] += 1
		if cs[0] == n // 3:
			break
if cs[2] < n // 3:
	for i in range(n - 1, -1, -1):
		if a[i] != 2 and cs[a[i]] > n // 3:
			cs[a[i]] -= 1
			a[i] = 2
			cs[2] += 1
		if cs[2] == n // 3:
			break
if cs[1] < n // 3:
	for i in range(n - 1, -1, -1):
		if a[i] == 0 and cs[0] > n // 3:
			cs[0] -= 1
			a[i] = 1
			cs[1] += 1
		if cs[1] == n // 3:
			break
	for i in range(n):
		if cs[1] == n // 3:
			break
		if a[i] == 2 and cs[2] > n // 3:
			cs[2] -= 1
			a[i] = 1
			cs[0] += 1
print(''.join(list(map(str, a))))


def main():
	n = int(input())
	s = [int(x) for x in input().strip()]
	counts = [0, 0, 0]
	for x in s:
		counts[x] += 1
	norm = sum(counts) // 3
	replaceable = [counts[x] - norm for x in range(3)]
	new_counts = [0, 0, 0]
	ans_arr = []
	for o in s:
		if replaceable[o] > 0:
			candidates = [i for (i, x) in enumerate(replaceable) if x < 0]
			picked = min(candidates)
			if picked > o and norm - new_counts[o] > 0:
				ans_arr.append(o)
				new_counts[o] += 1
				continue
			ans_arr.append(picked)
			replaceable[o] -= 1
			replaceable[picked] += 1
			new_counts[picked] += 1
		else:
			ans_arr.append(o)
			new_counts[o] += 1
	print(''.join([str(x) for x in ans_arr]))
main()


import collections
import time
import os
import sys
import bisect
import heapq
N = int(input())
S = [int(x) for x in list(input())]
wc = collections.Counter(S)
for v in [0, 1, 2]:
	if v not in wc:
		wc[v] = 0
counts = [(c, w) for (w, c) in wc.items()]
counts.sort()
d = N // 3
(ca, a) = counts[0]
(cb, b) = counts[1]
(cc, c) = counts[2]
ai = [i for i in range(N) if S[i] == a]
bi = [i for i in range(N) if S[i] == b]
ci = [i for i in range(N) if S[i] == c]
if ca == d and cb == d:
	print(''.join(map(str, S)))
elif ca <= d and cb <= d:
	s1 = [x for x in S]
	for i in ci[:d - ca]:
		s1[i] = a
	for i in ci[d - ca:2 * d - ca - cb]:
		s1[i] = b
	s2 = [x for x in S]
	for i in ci[:d - cb]:
		s2[i] = b
	for i in ci[d - cb:2 * d - ca - cb]:
		s2[i] = a
	s3 = [x for x in S]
	for i in ci[cc - d + ca:]:
		s3[i] = a
	for i in ci[cc - 2 * d + cb + ca:cc - d + ca]:
		s3[i] = b
	s4 = [x for x in S]
	for i in ci[cc - d + cb:]:
		s4[i] = b
	for i in ci[cc - 2 * d + cb + ca:cc - d + cb]:
		s4[i] = a
	s5 = [x for x in S]
	for i in ci[:d - ca]:
		s5[i] = a
	for i in ci[cc - d + cb:]:
		s5[i] = b
	s6 = [x for x in S]
	for i in ci[:d - cb]:
		s6[i] = b
	for i in ci[cc - d + ca:]:
		s6[i] = a
	ss = [''.join(map(str, s)) for s in [s1, s2, s3, s4, s5, s6]]
	ss.sort()
	print(ss[0])
else:
	cmb = cb - d
	cmc = cc - d
	s1 = [x for x in S]
	for i in bi[:cmb]:
		s1[i] = a
	for i in ci[:cmc]:
		s1[i] = a
	s2 = [x for x in S]
	for i in bi[cb - cmb:]:
		s2[i] = a
	for i in ci[:cmc]:
		s2[i] = a
	s3 = [x for x in S]
	for i in bi[:cmb]:
		s3[i] = a
	for i in ci[cc - cmc:]:
		s3[i] = a
	s4 = [x for x in S]
	for i in bi[cb - cmb:]:
		s4[i] = a
	for i in ci[cc - cmc:]:
		s4[i] = a
	ss = [''.join(map(str, s)) for s in [s1, s2, s3, s4]]
	ss.sort()
	print(ss[0])


from collections import *
from math import *
n = int(input())
s = list(input())
(c0, c1, c2) = (s.count('0'), s.count('1'), s.count('2'))
ct = n // 3
for i in range(n):
	if c0 >= ct:
		break
	if s[i] == '1' and c1 > ct:
		c0 += 1
		c1 -= 1
		s[i] = '0'
	if s[i] == '2' and c2 > ct:
		c0 += 1
		c2 -= 1
		s[i] = '0'
for i in range(n):
	if c1 >= ct:
		break
	if s[i] == '2' and c2 > ct:
		c1 += 1
		c2 -= 1
		s[i] = '1'
for i in range(n - 1, -1, -1):
	if c2 >= ct:
		break
	if s[i] == '0' and c0 > ct:
		c2 += 1
		c0 -= 1
		s[i] = '2'
	if s[i] == '1' and c1 > ct:
		c2 += 1
		c1 -= 1
		s[i] = '2'
for i in range(n - 1, -1, -1):
	if c1 >= ct:
		break
	if s[i] == '0' and c0 > ct:
		c1 += 1
		c0 -= 1
		s[i] = '1'
print(''.join(s))


n = int(input())
pos = list(input())
c0 = pos.count('0')
c1 = pos.count('1')
c2 = pos.count('2')
et = (c0 + c1 + c2) // 3
for item in range(len(pos)):
	if pos[item] == '2':
		if c2 > et and c0 < et:
			pos[item] = '0'
			c0 += 1
			c2 -= 1
		elif c2 > et and c1 < et:
			pos[item] = '1'
			c1 += 1
			c2 -= 1
	elif pos[item] == '1':
		if c1 > et and c0 < et:
			pos[item] = '0'
			c0 += 1
			c1 -= 1
pos.reverse()
for item in range(len(pos)):
	if pos[item] == '0':
		if c0 > et and c2 < et:
			pos[item] = '2'
			c2 += 1
			c0 -= 1
		elif c0 > et and c1 < et:
			pos[item] = '1'
			c1 += 1
			c0 -= 1
	elif pos[item] == '1':
		if c1 > et and c2 < et:
			pos[item] = '2'
			c2 += 1
			c1 -= 1
pos.reverse()
tot = ''
for item in pos:
	tot += str(item)
print(tot)


n = int(input())
s = input()
c = {0: s.count('0'), 1: s.count('1'), 2: s.count('2')}
p = list(s)
for i in range(n):
	if s[i] == '2' and c[2] > n / 3:
		if c[0] < n / 3:
			p[i] = '0'
			(c[2], c[0]) = (c[2] - 1, c[0] + 1)
		else:
			p[i] = '1'
			(c[2], c[1]) = (c[2] - 1, c[1] + 1)
	elif s[i] == '1' and c[1] > n / 3:
		if c[0] < n / 3:
			p[i] = '0'
			(c[1], c[0]) = (c[1] - 1, c[0] + 1)
for i in range(n - 1, -1, -1):
	if s[i] == '0' and c[0] > n / 3:
		if c[2] < n / 3:
			p[i] = '2'
			(c[2], c[0]) = (c[2] + 1, c[0] - 1)
		else:
			p[i] = '1'
			(c[1], c[0]) = (c[1] + 1, c[0] - 1)
	elif s[i] == '1' and c[1] > n / 3:
		if c[2] < n / 3:
			p[i] = '2'
			(c[2], c[1]) = (c[2] + 1, c[1] - 1)
print(''.join(p))


a = int(input())
s = str(input())
z = 0
o = 0
t = 0
for i in s:
	if i == '0':
		z += 1
	if i == '1':
		o += 1
	if i == '2':
		t += 1
num = a // 3
zn = num - z
on = num - o
tn = num - t
if z == o == t:
	print(s)
else:
	zm = 0
	om = 0
	tm = 0
	s = list(s)
	for i in range(0, a):
		if s[i] == '2':
			if tn < 0:
				if zn > 0:
					s[i] = '0'
					zn -= 1
					tn += 1
				else:
					s[i] = '1'
					on -= 1
					tn += 1
		if s[i] == '1':
			om += 1
			if on < 0:
				if zn > 0:
					s[i] = '0'
					zn -= 1
					on += 1
					om -= 1
				elif om <= num:
					continue
				else:
					s[i] = '2'
					tn -= 1
					on += 1
		if s[i] == '0':
			zm += 1
			if zn < 0:
				if zm <= num:
					continue
				elif on > 0:
					on -= 1
					s[i] = '1'
					zn += 1
				else:
					s[i] = '2'
					tn -= 1
					zn += 1
	s = ''.join(s)
	print(s)


from collections import Counter
n = int(input())
s = list(input())
c = Counter(s)
b_n = n // 3
for x in set(c.keys()).union(set('012')):
	c[x] -= b_n
left = '0'
right = '2'
mid = '1'

def process(ch, is_reversed=True, check=lambda ch, v: v != ch and c[v] > 0):
	global s, c
	if c[ch] < 0:
		r = (n - 1, -1, -1) if is_reversed else (n,)
		for i in range(*r):
			v = s[i]
			if check(ch, v):
				c[v] -= 1
				s[i] = ch
				c[ch] += 1
			if c[ch] == 0:
				break
process(left, is_reversed=False)
process(right)
if c[right] > 0:
	process(mid, check=lambda ch, v: v == right and c[v] > 0, is_reversed=False)
if c[left] > 0:
	process(mid, check=lambda ch, v: v == left, is_reversed=True)
print(''.join(s))


n = int(input())
s = list(input())
(a, b, c) = (0, 0, 0)
for ch in s:
	if ch == '0':
		a += 1
	elif ch == '1':
		b += 1
	else:
		c += 1
for i in range(n):
	if s[i] == '2' and c > n // 3:
		if a < n // 3:
			s[i] = '0'
			(a, c) = (a + 1, c - 1)
		elif b < n // 3:
			s[i] = '1'
			(b, c) = (b + 1, c - 1)
	elif s[i] == '1' and b > n // 3:
		if a < n // 3:
			s[i] = '0'
			(a, b) = (a + 1, b - 1)
for i in range(n - 1, -1, -1):
	if s[i] == '0' and a > n // 3:
		if c < n // 3:
			s[i] = '2'
			(c, a) = (c + 1, a - 1)
		elif b < n // 3:
			s[i] = '1'
			(b, a) = (b + 1, a - 1)
	elif s[i] == '1' and b > n // 3:
		if c < n // 3:
			s[i] = '2'
			(c, b) = (c + 1, b - 1)
print(''.join(s))


n = int(input())
s = list(map(int, list(input())))
l = [0, 0, 0]
for i in s:
	l[i] += 1
mid = n // 3
if l[0] > mid:
	for i in range(n - 1, -1, -1):
		if l[0] == mid:
			break
		if s[i] == 0:
			if l[2] < mid:
				s[i] = 2
				l[2] += 1
				l[0] -= 1
			elif l[1] < mid:
				s[i] = 1
				l[1] += 1
				l[0] -= 1
if l[1] > mid:
	for i in range(n):
		if l[1] == mid:
			break
		if s[i] == 1 and l[0] < mid:
			s[i] = 0
			l[1] -= 1
			l[0] += 1
if l[1] > mid:
	for i in range(n - 1, -1, -1):
		if l[1] == mid:
			break
		if s[i] == 1 and l[2] < mid:
			s[i] = 2
			l[1] -= 1
			l[2] += 1
if l[2] > mid:
	for i in range(n):
		if l[2] == mid:
			break
		if s[i] == 2:
			if l[0] < mid:
				s[i] = 0
				l[0] += 1
				l[2] -= 1
			elif l[1] < mid:
				s[i] = 1
				l[1] += 1
				l[2] -= 1
print(''.join(map(str, s)))


n = int(input())
s = str(input())
x = n // 3
(zer, on, tw) = ([], [], [])
(zero, one, two) = (0, 0, 0)
for i in range(n):
	if s[i] == '0':
		zero += 1
		zer.append(['0', i])
	elif s[i] == '1':
		one += 1
		on.append(['1', i])
	else:
		two += 1
		tw.append(['2', i])
if zero > x:
	if one < x and two < x:
		for i in range(x, 2 * x - one):
			zer[i][0] = '1'
		for i in range(2 * x - one, zero):
			zer[i][0] = '2'
	elif one < x and two >= x:
		for i in range(x, zero):
			zer[i][0] = '1'
		for i in range(2 * x - one - zero):
			tw[i][0] = '1'
	elif one >= x and two < x:
		for i in range(x, zero):
			zer[i][0] = '2'
		for i in range(x, one):
			on[i][0] = '2'
elif one > x:
	if zero < x and two < x:
		for i in range(x - zero):
			on[i][0] = '0'
		for i in range(2 * x - zero, one):
			on[i][0] = '2'
	elif zero < x and two >= x:
		for i in range(2 * x - zero - two):
			on[i][0] = '0'
		for i in range(2 * x - zero - one):
			tw[i][0] = '0'
	elif zero >= x and two < x:
		for i in range(x, one):
			on[i][0] = '2'
		for i in range(x, zero):
			zer[i][0] = '2'
elif two > x:
	if zero < x and one < x:
		for i in range(x - zero):
			tw[i][0] = '0'
		for i in range(x - zero, two - x):
			tw[i][0] = '1'
	elif zero < x and one >= x:
		for i in range(2 * x - zero - one):
			tw[i][0] = '0'
		for i in range(2 * x - zero - two):
			on[i][0] = '0'
	elif zero >= x and one < x:
		for i in range(2 * x - zero - one):
			tw[i][0] = '1'
		for i in range(x, zero):
			zer[i][0] = '1'
ans = [0] * n
for i in zer:
	ans[i[1]] = i[0]
for i in on:
	ans[i[1]] = i[0]
for i in tw:
	ans[i[1]] = i[0]
print(''.join(ans))


n = int(input())
s = input()
nums = [0] * 3
for v in s:
	nums[int(v)] += 1
s = list(s)
for i in range(n):
	if s[i] == '2' and nums[-1] > n // 3:
		if nums[0] < n // 3:
			s[i] = '0'
			nums[0] += 1
			nums[-1] -= 1
		elif nums[1] < n // 3:
			s[i] = '1'
			nums[1] += 1
			nums[-1] -= 1
	elif s[i] == '1' and nums[1] > n // 3:
		if nums[0] < n // 3:
			s[i] = '0'
			nums[0] += 1
			nums[1] -= 1
for i in range(n - 1, -1, -1):
	if s[i] == '0' and nums[0] > n // 3:
		if nums[2] < n // 3:
			s[i] = '2'
			nums[2] += 1
			nums[0] -= 1
		elif nums[1] < n // 3:
			s[i] = '1'
			nums[1] += 1
			nums[0] -= 1
	elif s[i] == '1' and nums[1] > n // 3:
		if nums[2] < n // 3:
			s[i] = '2'
			nums[2] += 1
			nums[1] -= 1
print(''.join(s))


n = int(input())
s = input()
ideal = n // 3
d = {}
d['0'] = s.count('0')
d['1'] = s.count('1')
d['2'] = s.count('2')
a = list(s)
for i in range(n):
	c = a[i]
	if d[c] > ideal:
		for j in range(int(c)):
			if d[str(j)] < ideal:
				d[str(j)] += 1
				d[a[i]] -= 1
				a[i] = str(j)
				break
for i in range(n - 1, -1, -1):
	c = a[i]
	if d[c] > ideal:
		for j in range(2, -1, -1):
			if d[str(j)] < ideal:
				d[str(j)] += 1
				d[a[i]] -= 1
				a[i] = str(j)
				break
print(''.join(a))


import math
from collections import defaultdict, Counter
import os
import sys
from io import BytesIO, IOBase
ii = lambda : int(input())
lmii = lambda : list(map(int, input().split()))
slmii = lambda : sorted(map(int, input().split()))
li = lambda : list(input())
mii = lambda : map(int, input().split())
msi = lambda : map(str, input().split())

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	return a * b // gcd(a, b)

def ncr(n, r):
	return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def npr(n, r):
	return math.factorial(n) // math.factorial(n - r)

def main():
	n = ii()
	s = li()
	d = defaultdict(int)
	for i in range(n):
		s[i] = int(s[i])
	for i in range(n):
		d[s[i]] += 1
	val = n // 3
	ele = 0
	if d[ele] < val:
		for i in range(n):
			if d[ele] == val:
				break
			if s[i] != ele:
				if d[s[i]] > val:
					d[s[i]] -= 1
					s[i] = ele
					d[ele] += 1
	ele = 2
	if d[ele] < val:
		for i in range(n - 1, -1, -1):
			if d[ele] == val:
				break
			if s[i] != ele:
				if d[s[i]] > val:
					d[s[i]] -= 1
					s[i] = ele
					d[ele] += 1
	ele = 1
	if d[ele] < val:
		for i in range(n):
			if d[ele] == val:
				break
			if s[i] == 2:
				if d[s[i]] > val:
					d[s[i]] -= 1
					s[i] = ele
					d[ele] += 1
	ele = 1
	if d[ele] < val:
		for i in range(n - 1, -1, -1):
			if d[ele] == val:
				break
			if s[i] == 0:
				if d[s[i]] > val:
					d[s[i]] -= 1
					s[i] = ele
					d[ele] += 1
	for i in range(n):
		print(s[i], end='')
	print()
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys
inf = float('inf')
mod = 1000000007

def get_array():
	return list(map(int, sys.stdin.readline().split()))

def get_ints():
	return map(int, sys.stdin.readline().split())

def input():
	return sys.stdin.readline()

def main():
	n = int(input())
	s = list(input().strip())
	from collections import Counter
	d = Counter(s)
	count_0 = d['0'] - n // 3
	count_1 = d['1'] - n // 3
	count_2 = d['2'] - n // 3
	if count_0 > 0:
		for i in range(n - 1, -1, -1):
			if s[i] == '0':
				count_0 -= 1
				if count_2 < 0:
					count_2 += 1
					s[i] = '2'
				elif count_1 < 0:
					count_1 += 1
					s[i] = '1'
			if count_0 == 0:
				break
	elif count_0 < 0:
		for i in range(n):
			if s[i] == '1' and count_1 > 0:
				count_0 += 1
				count_1 -= 1
				s[i] = '0'
			if s[i] == '2' and count_2 > 0:
				count_0 += 1
				count_2 -= 1
				s[i] = '0'
			if count_0 == 0:
				break
	if count_1 > 0:
		for i in range(n - 1, -1, -1):
			if s[i] == '1':
				count_1 -= 1
				count_2 += 1
				s[i] = '2'
			if count_1 == 0:
				break
	elif count_1 < 0:
		for i in range(n):
			if s[i] == '2':
				count_2 -= 1
				count_1 += 1
				s[i] = '1'
			if count_1 == 0:
				break
	print(*s, sep='')
main()


n = int(input())
s = input()
s = list(s)
import collections
c = collections.Counter(s)
num_balance = n // 3
(c0, c1, c2) = (0, 0, 0)
c0 = c['0'] - num_balance
c1 = c['1'] - num_balance
c2 = c['2'] - num_balance
for i in range(n):
	if s[i] == '2':
		if c2 > 0:
			if c0 < 0:
				s[i] = '0'
				c2 -= 1
				c0 += 1
			elif c1 < 0:
				s[i] = '1'
				c2 -= 1
				c1 += 1
for i in range(n):
	if s[i] == '1':
		if c1 > 0:
			if c0 < 0:
				s[i] = '0'
				c1 -= 1
				c0 += 1
for i in range(n):
	if s[n - i - 1] == '1':
		if c1 > 0:
			if c2 < 0:
				s[n - i - 1] = '2'
				c1 -= 1
				c2 += 1
for i in range(n):
	if s[n - i - 1] == '0':
		if c0 > 0:
			if c2 < 0:
				s[n - i - 1] = '2'
				c0 -= 1
				c2 += 1
			elif c1 < 0:
				s[n - i - 1] = '1'
				c0 -= 1
				c1 += 1
print(''.join(s))


n = int(input())
s = input()
k = n // 3
m0 = s.count('0')
m1 = s.count('1')
m2 = n - (m0 + m1)
k0 = 0
k1 = 0
k2 = 0
for a in s:
	if a == '0':
		if m0 <= k:
			print('0', end='')
		elif k0 < k:
			print('0', end='')
			k0 += 1
		elif m1 < k:
			print('1', end='')
			m1 += 1
			m0 -= 1
		else:
			print('2', end='')
			m2 += 1
			m0 -= 1
	if a == '1':
		if m1 <= k:
			print('1', end='')
		elif m0 < k:
			print('0', end='')
			m0 += 1
			m1 -= 1
		elif k1 < k:
			print('1', end='')
			k1 += 1
		else:
			print('2', end='')
			m2 += 1
			m1 -= 1
	if a == '2':
		if m2 <= k:
			print('2', end='')
		elif m0 < k:
			print('0', end='')
			m0 += 1
			m2 -= 1
		else:
			print('1', end='')
			m1 += 1
			m2 -= 1


n = int(input())
numstr = input()
dic = dict()
(dic['0'], dic['1'], dic['2']) = (0, 0, 0)
opti = int(n / 3)
for c in numstr:
	dic[c] += 1
ans = [c for c in numstr]
for i in range(len(numstr)):
	c = numstr[i]
	if c == '2' and dic['0'] < opti and (dic['2'] > opti):
		ans[i] = '0'
		dic['0'] += 1
		dic['2'] -= 1
	elif c == '2' and dic['1'] < opti and (dic['2'] > opti):
		ans[i] = '1'
		dic['1'] += 1
		dic['2'] -= 1
	elif c == '1' and dic['1'] > opti and (dic['0'] < opti):
		ans[i] = '0'
		dic['1'] -= 1
		dic['0'] += 1
for i in range(len(numstr) - 1, -1, -1):
	c = numstr[i]
	if c == '0' and dic['0'] > opti and (dic['2'] < opti):
		ans[i] = '2'
		dic['0'] -= 1
		dic['2'] += 1
	elif c == '0' and dic['0'] > opti and (dic['1'] < opti):
		ans[i] = '1'
		dic['0'] -= 1
		dic['1'] += 1
	elif c == '1' and dic['1'] > opti and (dic['2'] < opti):
		ans[i] = '2'
		dic['1'] -= 1
		dic['2'] += 1
ans = ''.join(ans)
print(ans)


n = int(input())
s = input()
zero = []
one = []
two = []
for i in range(n):
	if s[i] == '1':
		one.append(i)
	elif s[i] == '2':
		two.append(i)
	else:
		zero.append(i)
if len(zero) == n / 3:
	if len(one) == n / 3:
		pass
	elif len(one) > n / 3:
		for i in range(len(one) - 1, n // 3 - 1, -1):
			two.append(one[i])
			one[i] = -1
	else:
		for i in range(0, len(two) - n // 3):
			one.append(two[i])
			two[i] = -1
elif len(zero) > n / 3:
	if len(one) == n / 3:
		for i in range(len(zero) - 1, n // 3 - 1, -1):
			two.append(zero[i])
			zero[i] = -1
	elif len(one) < n / 3:
		if len(two) == n / 3:
			for i in range(len(zero) - 1, n // 3 - 1, -1):
				one.append(zero[i])
				zero[i] = -1
		elif len(two) < n / 3:
			t = 0
			et = n / 3 - len(two)
			for i in range(len(zero) - 1, n // 3 - 1, -1):
				two.append(zero[i])
				zero[i] = -1
				t += 1
				if t == et:
					ind = i - 1
					break
			t = 0
			et = n / 3 - len(one)
			for i in range(ind, n // 3 - 1, -1):
				one.append(zero[i])
				zero[i] = -1
				t += 1
				if t == et:
					break
		else:
			for i in range(0, len(two) - n // 3):
				one.append(two[i])
				two[i] = -1
			for i in range(len(zero) - 1, n // 3 - 1, -1):
				one.append(zero[i])
				zero[i] = -1
	else:
		for i in range(len(one) - 1, n // 3 - 1, -1):
			two.append(one[i])
			one[i] = -1
		for i in range(len(zero) - 1, n // 3 - 1, -1):
			two.append(zero[i])
			zero[i] = -1
elif len(one) == n / 3:
	for i in range(0, len(two) - n // 3):
		zero.append(two[i])
		two[i] = -1
elif len(one) > n / 3:
	if len(two) == n / 3:
		for i in range(len(one) - n // 3):
			zero.append(one[i])
			one[i] = -1
	elif len(two) < n / 3:
		t = 0
		et = n / 3 - len(zero)
		for i in range(0, len(one)):
			zero.append(one[i])
			one[i] = -1
			t += 1
			if t == et:
				ind = i + 1
				break
		t = 0
		et = n / 3 - len(two)
		for i in range(len(one) - 1, -1, -1):
			two.append(one[i])
			one[i] = -1
			t += 1
			if t == et:
				break
	else:
		for i in range(0, len(two) - n // 3):
			zero.append(two[i])
			two[i] = -1
		for i in range(0, len(one) - n // 3):
			zero.append(one[i])
			one[i] = -1
else:
	t = 0
	et = n / 3 - len(zero)
	for i in range(0, len(two)):
		zero.append(two[i])
		two[i] = -1
		t += 1
		if t == et:
			ind = i + 1
			break
	t = 0
	et = n / 3 - len(one)
	for i in range(ind, len(two)):
		one.append(two[i])
		two[i] = -1
		t += 1
		if t == et:
			break
ans = ['0'] * n
for i in one:
	if i != -1:
		ans[i] = '1'
for i in two:
	if i != -1:
		ans[i] = '2'
j = ''
for i in ans:
	j += i
print(j)


import sys
input = lambda : sys.stdin.readline().strip('\r\n')
n = int(input())
s = list(input())
(cnt_zero, cnt_one, cnt_two) = (s.count('0'), s.count('1'), s.count('2'))
i = 0
while i < n and cnt_zero < n // 3:
	if s[i] == '1' and cnt_one > n // 3:
		s[i] = '0'
		cnt_zero += 1
		cnt_one -= 1
	elif s[i] == '2' and cnt_two > n // 3:
		s[i] = '0'
		cnt_zero += 1
		cnt_two -= 1
	i += 1
i = n - 1
while i >= 0 and cnt_two < n // 3:
	if s[i] == '0' and cnt_zero > n // 3:
		s[i] = '2'
		cnt_two += 1
		cnt_zero -= 1
	elif s[i] == '1' and cnt_one > n // 3:
		s[i] = '2'
		cnt_two += 1
		cnt_one -= 1
	i -= 1
i = 0
while i < n and cnt_one < n // 3:
	if s[i] == '2' and cnt_two > n // 3:
		s[i] = '1'
		cnt_one += 1
		cnt_two -= 1
	i += 1
i = n - 1
while i >= 0 and cnt_one < n // 3:
	if s[i] == '0' and cnt_zero > n // 3:
		s[i] = '1'
		cnt_one += 1
		cnt_zero -= 1
	i -= 1
print(''.join(s))