#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:167772160")
using namespace std;
int M[1010][1010] = {}, a[1010][1010] = {};
int main() {
  int n, m, x, y, ans = 1000000000;
  scanf("%d%d", &n, &m);
  for (int(i) = 0; (i) < (m); ++(i)) {
    scanf("%d%d", &x, &y);
    if (M[x][y]) continue;
    for (int(dx) = 0; (dx) < (3); ++(dx))
      for (int(dy) = 0; (dy) < (3); ++(dy))
        if (++a[x + dx][y + dy] == 9) ans = min(ans, i + 1);
  }
  printf("%d\n", ans == 1000000000 ? -1 : ans);
  return 0;
}
