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