#include "paint.h"
#include <bits/stdc++.h>
using namespace std;
int minimumInstructions(int n, int m, int k, vector<int> c, vector<int> a, vector<vector<int>> b){
bool st[n];
memset(st, 0, sizeof st);
vector<unordered_map<int, bool>> cl(k);
for(int i = 0;i<m;i++){
for(int j = 0;j<a[i];j++){
cl[b[i][j]][i] = 1;
}
}
for(int i = 0;i<n - m + 1;i++){
vector<int> cand;
for(int j = 0;j<m;j++) cand.push_back(j);
for(int j = 0;j<m;j++){
vector<int> ncd;
for(auto x : cand){
if(cl[c[i + j]][(x + j) % m]){
ncd.push_back(x);
}
}
swap(ncd, cand);
}
st[i] = !cand.empty();
}
int ans = 0, id = 0, pv = 0;
while(id != n){
if(st[id]){
pv = id;
++ans;
id += m;
}else{
--id;
if(id < 0 || id == pv) return -1;
}
}
return ans;
}