#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef std::vector<long long> vll;
typedef std::vector<std::vector<long long>> vvll;
typedef std::vector<bool> vb;
typedef std::vector<std::vector<bool>> vvb;
typedef std::vector<string> vstr;
typedef std::pair<long long, long long> pll;
#define INF 1999999999
#define INFLL std::numeric_limits<long long>::max()
#define MODA 1000000007
#define rep(i,n) for (long long i = 0; i < (n); ++i)
#define rep1(i,n) for (long long i = 1; i <= (n); ++i)
#define all(x) (x).begin(),(x).end()
#define errv1(x) cerr << #x <<" "<< x << endl;
#define errv2(x, y) cerr << #x <<" "<< x <<" "<< #y <<" "<< y << endl;
#define errv3(x, y, z) cerr << #x <<":"<< x <<" "<< #y <<":"<< y <<" "<< #z <<":"<< z << endl;
#define errv4(x, y, z, a) cerr << #x <<":"<< x <<" "<< #y <<":"<< y <<" "<< #z <<":"<< z <<" "<< #a <<":"<< a << endl;

int main() {
  ll H, W;
  cin >> H >> W;
  vvll A(H, vll(W));
  rep(i,H){
    rep(j,W){
      cin >> A[i][j];
    }
  }
  
  ll mini = INFLL;
  rep(i,H){
    rep(j,W){
      mini = min(mini, A[i][j]);
    }
  }
  
  ll ans =0;
  rep(i,H){
    rep(j,W){
      ans += A[i][j] - mini;
    }
  }
  cout << ans << endl;
  return 0;                                                                                                               
}

#include <cmath>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include<cstdio>
#include<functional>
#include <bitset>
#include <iomanip>
#include <cctype>
#include <list>
#include <cassert>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for(ll i = n; i >= 0; i--)
#define ll long long
#define repi(i,a,b) for(ll i=a;i<b;++i)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()

using namespace std;
template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }
template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
const ll INF = 1000000000000000000;
const ll MOD = 1e9 + 7;
using vvv = vector<vector<vector<ll>>>;
using vv = vector<vector<ll>>;
using vec = vector<ll>;
using P = pair<ll, ll>;

int main(void) {
	ll n; cin >> n;
	ll ans = INF;
	rep(i, n) {
		ll a, p, x; cin >> a >> p >> x;
		if (x - a > 0) ans = min(ans, p);
	}
	if (ans == INF) ans = -1;
	cout << ans << endl;
	system("pause");
}