#include <bits/stdc++.h>
using namespace std;
int n, m, dai, dem, vt, ck, max1, min1, kq;
int a[100005];
int d[1005][1005];
int check(int x0, int y0) {
  int i, j;
  if (x0 <= 0 || y0 <= 0) return 0;
  for (i = x0; i < x0 + 3; i++) {
    for (j = y0; j < y0 + 3; j++) {
      if (d[i][j] == 0) return 0;
    }
  }
  return 1;
}
int main() {
  int i, j1, j2, k, l;
  while (scanf("%d %d", &n, &m) > 0) {
    ck = -1;
    for (i = 1; i <= m; i++) {
      scanf("%d %d", &k, &l);
      d[k][l] = 1;
      if (ck == -1) {
        for (j1 = k - 2; j1 <= k + 2; j1++) {
          for (j2 = l - 2; j2 <= l + 2; j2++) {
            if (check(j1, j2) == 1) {
              cout << i << endl;
              ck = 1;
              break;
            }
          }
        }
      }
    }
    if (ck == -1) cout << "-1\n";
  }
  return 0;
}
