#include using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) using ll = long long; using ull = unsigned long long; #include using mint = atcoder::modint998244353; int main() { cin.tie(nullptr)->sync_with_stdio(false); int n; cin >> n; vector a(n); rep(i, n) cin >> a[i]; if (n % 2 == 1) { sort(a.begin(), a.end()); mint x = 0, y = 0; rep(i, (n + 1) / 2) x = x * 10 + a[i]; reverse(a.begin(), a.end()); rep(i, n / 2) y = y * 10 + a[i]; cout << (x - y).val() << '\n'; } else { vector cnt(10, 0); rep(i, n) cnt[a[i]]++; vector rem; rep(i, 10) if (cnt[i] % 2 == 1) rem.push_back(i); assert(rem.size() % 2 == 0); int ans = 1e9; do { int x = 0, y = 0; rep(i, rem.size()) { if (i < rem.size() / 2) x = x * 10 + rem[i]; else y = y * 10 + rem[i]; } ans = min(ans, abs(x - y)); } while (next_permutation(rem.begin(), rem.end())); cout << ans << '\n'; } return 0; }