#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1024;
int n, m;
bool b[MAXN][MAXN];
int main() {
  cin >> n >> m;
  for (register int l = (1); l < (int)(m + 1); ++l) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    b[x][y] = true;
    for (register int xx = (x - 2); xx < (int)(x + 1); ++xx)
      for (register int yy = (y - 2); yy < (int)(y + 1); ++yy)
        if (xx >= 0 && yy >= 0) {
          bool ok = true;
          for (register int i = (0); i < (int)(3); ++i)
            for (register int j = (0); j < (int)(3); ++j)
              if (!b[i + xx][j + yy]) ok = false;
          if (ok) {
            cout << l << endl;
            return 0;
          }
        }
  }
  cout << -1 << endl;
  return 0;
}
