#include <bits/stdc++.h>
using namespace std;
int a[1020][1020] = {0}, b[1020][1020] = {0}, c[1020][1020] = {0};
int main() {
  int px[10] = {-2, -2, -2, -1, -1, -1, 0, 0, 0};
  int py[10] = {-2, -1, 0, -2, -1, 0, -2, -1, 0};
  int n, k, i, j, x, y, ans;
  scanf("%d%d", &n, &k);
  if (n < 3) {
    printf("-1\n");
    return 0;
  }
  for (i = 0; i < k; i++) {
    scanf("%d%d", &x, &y);
    x += 3;
    y += 3;
    c[x][y] = 1;
    for (j = 0; j < 9; j++)
      b[x - px[j]][y - py[j]] = max(b[x - px[j]][y - py[j]], i + 1);
  }
  for (i = 2; i <= n + 3; i++)
    for (j = 2; j <= n + 3; j++)
      a[i][j] = a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1] + c[i][j];
  ans = k + 1;
  for (i = 2; i <= n + 1; i++)
    for (j = 2; j <= n + 1; j++)
      if (b[i + 2][j + 2] < ans) {
        if (a[i + 2][j + 2] - a[i - 1][j + 2] - a[i + 2][j - 1] +
                a[i - 1][j - 1] ==
            9)
          ans = b[i + 2][j + 2];
      }
  if (ans == k + 1)
    printf("-1\n");
  else
    printf("%d\n", ans);
}
