#include <iostream>
#include <algorithm>
using namespace std;
int a[300];
long long ans[10000000];
int main()
{
long long b , e;
long long n;
cin >> n;
long long x , y , cnt = 0;
cin >> x;
if (n == 4)
{
cout << 6174;
return 0;
}
while (true)
{
cnt++;
int i = 1;
while (x > 0)
{
a[i] = x % 10;
x /= 10;
i++;
}
sort(a + 1, a + n + 1);
for (int i = 1; i <= n; i++)
{
x = x * 10 + a[i];
y = y * 10 + a[n-i+1];
}
x = y - x;
y = 0;
ans[cnt] = x;
for (int i = 1; i <= cnt - 1; i++)
{
if (ans[cnt] == ans[i])
{
b = i;
e = cnt - 1;
for (int j = b; j <= e; j++)
{
cout << ans[j] << ' ';
}
return 0;
}
}
}
return 0;
}