#include <bits/stdc++.h>
using namespace std;
bool a[1001][1001] = {{false}};
int n, m;
bool check(int i, int j) {
  for (int ii = i; ii <= i + 2; ii++) {
    if (ii < 1 || ii > n) return false;
    for (int jj = j; jj <= j + 2; jj++) {
      if (jj < 1 || jj > n) return false;
      if (!a[ii][jj]) return false;
    }
  }
  return true;
}
int main() {
  cin >> n >> m;
  int x, y;
  bool flag = false;
  int ans = -1;
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &x, &y);
    if (flag) continue;
    a[x][y] = true;
    if (check(x, y) || check(x - 1, y) || check(x - 2, y) ||
        check(x - 1, y - 1) || check(x, y - 1) || check(x - 2, y - 1) ||
        check(x - 1, y - 2) || check(x, y - 2) || check(x - 2, y - 2)) {
      ans = i + 1;
      flag = true;
    }
    if (check(x, y) || check(x + 1, y) || check(x + 2, y) ||
        check(x + 1, y + 1) || check(x, y + 1) || check(x + 2, y + 1) ||
        check(x + 1, y + 2) || check(x, y + 2) || check(x - 2, y + 2)) {
      ans = i + 1;
      flag = true;
    }
  }
  cout << ans;
  return 0;
}
