#include<iostream>
#include<vector>
#include<algorithm>
int main()
{
	int n;
	std::cin >> n;
	std::vector<int>num_array(8, 0);
	for (int i = 0; i < n; i++)
	{
		int num;
		std::cin >> num;
		for (int j = 0; j < 8; j++)
		{
			num_array[j] = num % 10;
			num /= 10;
		}
	}
	std::sort(num_array.begin(), num_array.end());
	const int maximum =
		num_array[0] +
		num_array[1] * 10 +
		num_array[2] * 100 +
		num_array[3] * 1000 +
		num_array[4] * 10000 +
		num_array[5] * 100000 +
		num_array[6] * 1000000 +
		num_array[7] * 10000000,
		minimum =
		num_array[0] * 10000000 +
		num_array[1] * 1000000 +
		num_array[2] * 100000 +
		num_array[3] * 10000 +
		num_array[4] * 1000 +
		num_array[5] * 100 +
		num_array[6] * 10 +
		num_array[7];
	std::cout << maximum - minimum << std::endl;
	return 0;
}
