#include "paint.h"
// author: yanybayev
#include "bits/stdc++.h"
using namespace std;
#define ff first
#define ss second
#define pp pop_back
#define ll long long
#define pb push_back
#define ls(v) (int)v.size()
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define wr cout << "------------------------" << endl
int minimumInstructions( int N, int M, int K, vector<int> C, vector<int> A, vector<vector<int>> B) {
vector<vector<char>> can(K, vector<char>(M, 0));
for (int i = 0;i<M;++i) {
for (auto it : B[i]) {
can[it][i] = 1;
}
}
vector<int> isvalid(N, 0);
for (int y = 0;y<=N - M;++y) {
bool ok = false;
for (int x = 0;x < M and !ok;++x) {
int good = true;
for (int l = 0;l < M;++l) {
int worker = (x + l) % M;
if (!can[C[y + l]][worker]) {
good = false;
break;
}
}
if (good) {ok = true; break; }
}
isvalid[y] = ok;
}
int ans, pos;
ans = pos = 0;
while (pos < N) {
int best = -1;
int L = max(0, pos - M + 1);
int R = min(pos, N - M);
for (int y = L; y <= R; ++ y) if (isvalid[y]) best = max(best, y + M);
if (best == -1) return -1;
ans += 1;
pos = best;
}
return ans;
}