#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;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmNvbnN0IGludCBtYXhuPTFlNysxNDsKbG9uZyBsb25nIG4sIG0sIG1pbnZhbHVlID0gMWUxOCAsIG1heHZhbHVlID0gLSAxZTE4Owp2b2lkIHNvbHZlKCl7CgljaW4gPj4gbiA+PiBtOwoJZm9yIChpbnQgaSA9IDE7IGkgPD0gbjsgaSsrKXsKCQlmb3IgKGludCBqID0gMTsgaiA8PSBtOyBqKyspIHsKCQkJbG9uZyBsb25nIHg7CgkJCWNpbiA+PiB4OwoJCQltaW52YWx1ZSA9IG1pbihtaW52YWx1ZSwgeCk7CgkJCW1heHZhbHVlID0gbWF4KG1heHZhbHVlLCB4KTsKCQl9Cgl9CgkKCWNvdXQgPDwgbWF4dmFsdWUgLSBtaW52YWx1ZTsKfQppbnQgbWFpbigpewoJaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbygwKTsKCWNpbi50aWUoMCk7ICAgIGNvdXQudGllKDApOwoJaWYgKGZvcGVuKCJpbnB1dC50eHQiLCJyIikpIHsKCQlmcmVvcGVuKCJpbnB1dC50eHQiLCJyIixzdGRpbik7CgkJZnJlb3Blbigib3V0cHV0LnR4dCIsInciLHN0ZG91dCk7Cgl9Cglzb2x2ZSgpOwoJcmV0dXJuIDA7Cn0K