#include <bits/stdc++.h>
using namespace std;
int cap[1005][1005];
int main(int argc, char** argv) {
  int n, m;
  cin >> n >> m;
  int a, b;
  int cs = 0;
  memset(cap, 0, sizeof(cap));
  while (m--) {
    cs++;
    scanf("%d%d", &a, &b);
    cap[a][b] = cs;
  }
  int ans = 1000000;
  bool ok = false;
  int tmp = 1000000;
  for (int i = (1); i < (n - 1); i++) {
    for (int j = (1); j < (n - 1); j++) {
      tmp = -1;
      ok = false;
      for (int k = (i); k < (i + 3); k++) {
        for (int t = (j); t < (j + 3); t++) {
          if (!cap[k][t]) {
            ok = 1;
            break;
          }
          tmp = max(tmp, cap[k][t]);
        }
        if (ok) break;
      }
      if (!ok) {
        ans = min(ans, tmp);
      }
    }
  }
  if (ans == 1000000)
    printf("-1\n");
  else
    printf("%d\n", ans);
  return 0;
}
