#include<bits/stdc++.h> usingnamespace std; #ifdef LOCAL #define debug(...) fprintf(stdout, ##__VA_ARGS__) #else #define debug(...) void(0) #endif #define int long long inlineintread(){int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+ (x<<3)+(ch^48);ch=getchar();}return x*f;} #define Z(x) (x)*(x) #define pb push_back #define fi first #define se second //#define M //#define mo #define N 200010 int n, m, i, j, k, T; int d, nw, id, ans, sum, mod, flg; int mq, dis[N], u, v; queue<int>q;
signedmain() { freopen("card.in", "r", stdin); freopen("card.out", "w", stdout); // srand(time(NULL)); // T=read(); // while(T--) { // // } n = read(); m = read(); for(i = 2, mod = 1; i <= 2 * n; i += 2) mod *= i; --mod; d = mod; for(i = 1, j = 1; i <= n; j *= (2 * i), ++i) { k = read(); sum += k * j % mod; sum %= mod; if(k) flg = 1; } for(id = 1; id <= m; ++id) { nw = 0; for(i = 1, j = 1; i <= n; j *= (2 * i), ++i) { k = read(); nw += k * j % mod; nw %= mod; } if(!nw) continue; d = __gcd(d, nw); } /*******************************************/
auto calc = [&] (int x) -> int{ int ans = 0, i; for(i = 2; i <= 2 * n; i += 2) ans += x % i, x /= i; if(ans == 0) return mod; return ans; }; mq = sqrt(sum); ans = calc(sum); if(!flg) ans = 0; if(d >= mq) { for(i = 0; i * d <= mod; ++i) ans = min(ans, calc((sum + i * d) % mod)); printf("%lld", ans); } else { sum %= d; memset(dis, 0x3f, sizeof(dis)); dis[0] = 0; q.push(0); while(!q.empty()) { u = q.front(); q.pop(); for(i = j = 1; i <= n; j *= (2 * i), ++i) { v = (u + j) % d; if(v == 0) v = d; if(dis[u] + 1 < dis[v]) { dis[v] = dis[u] + 1; q.push(v); } } } if(sum == 0) sum = d; debug(">> %lld\n", sum); printf("%lld\n", min(ans, dis[sum])); } /*******************************************/ return0; }