#define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>

using namespace std;

typedef long long ll;
typedef pair<int,int> P;


int main(){
	double eps = 10e-9;
	
	int N;
	scanf("%d",&N);
	vector<vector<int>> num_as;
	vector<vector<int>> num_des;
	for(int n=0;n<N;n++){
		string str;
		cin >> str;
		vector<int> tmp;
		for(int i=0;i<str.size();i++){
			tmp.push_back(str[i]-'0');
		}
		sort(tmp.begin(),tmp.end());
		num_as.push_back(tmp);

		sort(tmp.begin(),tmp.end(),greater<int>());
		num_des.push_back(tmp);
	}

	int a=0;
	for(int i=num_as[0].size()-1;i>=0;i--){
		a += num_as[0][i] * (pow(10,(double)i) + eps);
	}

	int b=0;
	for(int i=num_des[0].size()-1;i>=0;i--){
		b += num_des[0][i] * (pow(10,(double)i) + eps);
	}

	cout << a - b << endl;
}