#include <bits/stdc++.h>
using namespace std;
int cell[1001][1001];
int main() {
  int n, m;
  cin >> n >> m;
  memset(cell, 0, sizeof(cell));
  for (int(k) = (int)(0); (k) < (int)(m); ++(k)) {
    int i, j;
    cin >> i >> j;
    --i, --j;
    ++cell[i][j];
    for (int di = -2; di <= 2; ++di)
      for (int dj = -2; dj <= 2; ++dj) {
        int i2 = i + di, j2 = j + dj;
        if (!(0 <= i2 && i2 < n && 0 <= j2 && j2 < n)) continue;
        int s = 0;
        for (int(p) = (int)(0); (p) < (int)(3); ++(p))
          for (int(q) = (int)(0); (q) < (int)(3); ++(q))
            if (i2 + p < n && j2 + q < n) s += cell[i2 + p][j2 + q];
        if (s == 9) {
          cout << (k + 1) << endl;
          return 0;
        }
      }
  }
  cout << "-1" << endl;
  return 0;
}
