#include <bits/stdc++.h>
using namespace std;
const int maxn=1e7+14;
long long n, m, minvalue = 1e18 , maxvalue = - 1e18;
void solve(){
	cin >> n >> m;
	for (int i = 1; i <= n; i++){
		for (int j = 1; j <= m; j++) {
			long long x;
			cin >> x;
			minvalue = min(minvalue, x);
			maxvalue = max(maxvalue, x);
		}
	}
	
	cout << maxvalue - minvalue;
}
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);    cout.tie(0);
	if (fopen("input.txt","r")) {
		freopen("input.txt","r",stdin);
		freopen("output.txt","w",stdout);
	}
	solve();
	return 0;
}
