#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, i, j, k, x, y, f = 0;
  cin >> n >> m;
  int a[n + 2][n + 2];
  for (i = 0; i < n + 2; i++)
    for (j = 0; j < n + 2; j++) a[i][j] = 0;
  for (k = 1; k <= m; k++) {
    cin >> x >> y;
    for (i = -1; i < 2; i++)
      for (j = -1; j < 2; j++)
        if ((++a[x + i][y + j] == 9) && (!f)) f = k;
  };
  if (f)
    cout << f;
  else
    cout << -1;
  return 0;
}
