#include <bits/stdc++.h>
const int dr[9] = {0, 1, 1, 1, 0, -1, -1, -1, 0};
const int dc[9] = {1, 1, 0, -1, -1, -1, 0, 1, 0};
int N, M;
int B[(1024)][(1024)];
inline int inrange(int r, int c) {
  return r >= 1 && r <= N && c >= 1 && c <= N;
}
int main() {
  int i, j, k;
  scanf("%d %d", &N, &M);
  for (i = 1; i <= M; ++i) {
    int r, c;
    scanf("%d %d", &r, &c);
    B[r][c] = 1;
    if (i < 9) continue;
    for (k = 0; k < 9; ++k) {
      int r1 = r + dr[k];
      int c1 = c + dc[k];
      if (inrange(r1, c1) && B[r1][c1]) {
        if (r1 >= 2 && r1 + 1 <= N && c1 >= 2 && c1 + 1 <= N) {
          int count = 0;
          for (j = 0; j < 8; ++j) {
            int rr = r1 + dr[j];
            int cc = c1 + dc[j];
            if (inrange(rr, cc) && B[rr][cc])
              ++count;
            else
              break;
          }
          if (count == 8) {
            printf("%d\n", i);
            return 0;
          }
        }
      }
    }
  }
  printf("-1\n");
  return 0;
}
