#include <bits/stdc++.h>
using namespace std;
int main() {
  bool flag = 0;
  int c = INT_MAX;
  int ax[3][3] = {{0, 1, 2}, {-1, 0, 1}, {-2, -1, 0}};
  int **ar;
  int cou;
  int n, m, i, j, x, y, nx, ny, k, l, o;
  cin >> n >> m;
  ar = (int **)calloc(n, sizeof(int *));
  for (i = 0; i < n; i++) {
    ar[i] = (int *)calloc(n, sizeof(int));
  }
  for (i = 0; i < m; i++) {
    cin >> x >> y;
    x--;
    y--;
    ar[x][y] = 1;
    for (k = 0; k < 3; k++) {
      for (j = 0; j < 3; j++) {
        flag = 0;
        cou = 0;
        for (l = 0; l < 3; l++) {
          nx = x + ax[k][l];
          for (o = 0; o < 3; o++) {
            ny = y + ax[j][o];
            if (nx < 0 || ny < 0 || nx >= n || ny >= n || ar[nx][ny] == 0)
              break;
            cou++;
          }
        }
        if (cou == 9) {
          flag = 1;
          break;
        }
      }
      if (flag) break;
    }
    if (flag) {
      i++;
      c = i;
      break;
    }
  }
  for (; i < m; i++) cin >> x >> y;
  if (c == INT_MAX)
    cout << "-1\n";
  else
    cout << c << endl;
  return 0;
}
