#include <bits/stdc++.h>
using namespace std;
bool mp[1000][1000] = {false};
int n;
bool find(int x, int y) {
  int stX = max(0, x - 2), stY = max(0, y - 2);
  bool bl;
  for (int i = stX; i <= x; i++) {
    for (int j = stY; j <= y; j++) {
      bl = true;
      for (int w = 0; w < 3; w++) {
        for (int e = 0; e < 3; e++) {
          bl = bl && mp[i + w][j + e];
        }
      }
      if (bl) {
        return true;
      }
    }
  }
  return false;
}
int main() {
  ios_base::sync_with_stdio(false);
  int m;
  cin >> n >> m;
  int ans = 0, x, y;
  while (m--) {
    ans++;
    cin >> x >> y;
    x--;
    y--;
    mp[x][y] = true;
    if (find(x, y)) {
      cout << ans;
      return 0;
    }
  }
  cout << -1;
  return 0;
}
