#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<long long> vll;
typedef pair<int,int> pint;
typedef pair<long long, long long> pll;

#define MP make_pair
#define PB push_back
#define ALL(s) (s).begin(),(s).end()
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) 
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P) 
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) 
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s; }



long long gcd(long long a, long long b) {
    if (b == 0) return a;
    else return gcd(b, a % b);
}

long long lcm(long long a, long long b) {
    long long g = gcd(a, b);
    return a * b / g;
}

long long mod(long long a, long long m) {
    return (a % m + m) % m;
}

inline long long inv(long long a, long long m) {
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a/b;
        a -= t*b; swap(a, b);
        u -= t*v; swap(u, v);
    }
    return mod(u, m);
}

pair<long long, long long> ChineseRem(vector<long long> vb, vector<long long> vm) {
    long long x = 0, m = 1, a, b, d;
    for (int i = 0; i < vb.size(); ++i) {
        a = m, b = vb[i] - x, d = gcd(vm[i], a);
        if (b % d != 0) return make_pair(0, -1);
        long long t = mod(b/d * inv(a/d, vm[i]/d), vm[i]/d);
        x += m * t;
        m *= vm[i]/d;
    }
    return make_pair(x % m, m);
}




int N, M, D;
int A[12];
int r;

int main() {
    while (cin >> N >> M >> D) {
        for (int i = 0; i < M; ++i) cin >> A[i];
        
        bool ok = true;
        for (int i = 0; i < D; ++i) {
            vector<ll> vb, vm;
            for (int j = 0; j < M; ++j) {
                cin >> r;
                if (r == -1) continue;
                vb.PB(r);
                vm.PB(A[j]);
            }
            
            pint res = ChineseRem(vb, vm);
            int rem = res.first;
            int mo = res.second;
//            
//            COUT(vb); COUT(vm);
//            COUT(res);
            
            if (mo == -1) ok = false;
            if (N < rem) ok = false;
            
            int NN = N - rem;
            int r = NN%mo;
            N = N - r;
        }
        
        if (ok) cout << N << endl;
        else cout << -1 << endl;
    }
    return 0;
}


Measured memory_bytes: 1220