#include using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) using ll = long long; using ull = unsigned long long; int main() { cin.tie(nullptr)->sync_with_stdio(false); int n; cin >> n; vector a(n); rep(i, n) cin >> a[i]; vector cnt(10, 0); rep(i, n) cnt[a[i]]++; vector rem, us; rep(i, 10) { if (cnt[i] % 2 == 1) rem.push_back(i); if (cnt[i] >= 2) us.push_back(i); } int ans = 1e9; int minim = us.empty() ? 0 : *min_element(us.begin(), us.end()); do { int x = 0, y = 0; if (rem.size() % 2 == 1) y = minim; 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; }