#include <bits/stdc++.h>
using namespace std;
const int maxn = 1005;
int a[maxn][maxn];
int s[maxn][maxn];
int x[maxn], y[maxn];
int n, m;
void init(int mid) {
  memset(s, 0, sizeof(s));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++)
      s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + (a[i][j] <= mid);
  }
}
int getSum(int x1, int y1, int x2, int y2) {
  return s[x2][y2] - s[x1 - 1][y2] - s[x2][y1 - 1] + s[x1 - 1][y1 - 1];
}
int main() {
  while (scanf("%d%d", &n, &m) != EOF) {
    memset(a, 0x7f, sizeof(a));
    for (int i = 1; i <= m; i++) {
      scanf("%d%d", &x[i], &y[i]);
      a[x[i]][y[i]] = i;
    }
    int l = 1, r = m, ans = -1;
    while (l <= r) {
      int mid = (l + r) / 2;
      init(mid);
      bool flag = false;
      for (int x1 = 1; x1 + 2 <= n; x1++) {
        for (int y1 = 1; y1 + 2 <= n; y1++) {
          int x2 = x1 + 2, y2 = y1 + 2;
          if (getSum(x1, y1, x2, y2) == 9) {
            flag = true;
            break;
          }
        }
        if (flag) break;
      }
      if (flag) {
        ans = mid;
        r = mid - 1;
      } else
        l = mid + 1;
    }
    printf("%d\n", ans);
  }
  return 0;
}
