#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  int matrix[n + 2][n + 2];
  memset(matrix, 0, sizeof matrix);
  int ind = 1e9;
  for (int k = 0; k < m; k++) {
    int i, j;
    cin >> i >> j;
    matrix[i][j] = k + 1;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      int cells[9] = {
          matrix[i - 1][j - 1], matrix[i - 1][j],     matrix[i - 1][j + 1],
          matrix[i][j + 1],     matrix[i + 1][j + 1], matrix[i + 1][j],
          matrix[i + 1][j - 1], matrix[i][j - 1],     matrix[i][j]};
      bool sol = true;
      int mx = -1;
      for (int k = 0; k < 9; k++) {
        int cell = cells[k];
        if (cell == 0) sol = false;
        mx = max(cell, mx);
      }
      if (!sol) continue;
      ind = min(mx, ind);
    }
  }
  if (ind == 1e9)
    cout << -1 << endl;
  else
    cout << ind << endl;
}
