#include <bits/stdc++.h>
#define INF 5000000000000000000
#define ll long long
#define pll pair<ll, ll>
using namespace std;

ll N, M;

ll count_max(vector<queue<ll>>& A, vector<bool>& already)
{
  vector<ll> cnt(M, 0);
  for (ll i = 0; i < N; ++i) {
    while (true) {
      if (already.at(A.at(i).front())) {
        A.at(i).pop();
      }
      else {
        cnt.at(A.at(i).front()) += 1;
        break;
      }
    }
  }
  ll res = 0, index;
  for (ll i = 0; i < M; ++i) {
    if (res < cnt.at(i)) {
      index = i;
      res = cnt.at(i);
    }
  }
  already.at(index) = true;
  return res;
}

int main()
{
  cin >> N >> M;
  ll ans = INF;
  vector<bool> already(M, false);
  vector<queue<ll>> A(N);
  for (ll i = 0; i < N; ++i) {
    for (ll j = 0; j < M; ++j) {
      ll a;
      cin >> a;
      a -= 1;
      A.at(i).push(a);
    }
  }
  for (ll i = 0; i < M; ++i) {
    ans = min(ans, count_max(A, already));
  }
  cout << ans << endl;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
using P = pair<string,int>;
const double PI = acos(-1);

int main() {
int l,m,n;
cin >> l >> m >> n;
vector<int> a(l);
vector<int> b(m);
rep(i,l) cin >> a.at(i);
rep(i,m) cin >> b.at(i);
vector<vector<int>> x(n,vector<int>(3));

rep(i,n)rep(j,3) cin >> x.at(i).at(j);
int ans = 100000000000000000;
int t;
rep(i,n){
  t = a.at(x.at(i).at(0)-1)+b.at(x.at(i).at(1)-1)-x.at(i).at(2);
  ans = min(ans,t);
}
sort(a.begin(),a.end());
sort(b.begin(),b.end());
  if(a.at(0)+b.at(0) < ans) ans = a.at(0)+b.at(0);
cout << ans << endl;
}
