#include <bits/stdc++.h>
using namespace std;
int n, m, vis[1005][1005];
int func(int x, int y, int a[9][9], int b[9][9]) {
  int c = 0;
  for (int i = 0; i < 9; i++) {
    c = 0;
    for (int j = 0; j < 9; j++) {
      if (x + a[i][j] <= n && y + b[i][j] > 0 && x + a[i][j] > 0 &&
          y + b[i][j] <= n && vis[x + a[i][j]][y + b[i][j]] == 1) {
        c++;
      }
    }
    if (c == 9) {
      return 1;
    }
  }
  return 0;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int a[9][9] = {
      {0, 0, 0, 1, 1, 1, 2, 2, 2},       {-1, -1, -1, 0, 0, 0, 1, 1, 1},
      {-2, -2, -2, -1, -1, -1, 0, 0, 0}, {0, 0, 0, 1, 1, 1, 2, 2, 2},
      {-1, -1, -1, 0, 0, 0, 1, 1, 1},    {-2, -2, -2, -1, -1, -1, 0, 0, 0},
      {0, 0, 0, 1, 1, 1, 2, 2, 2},       {-1, -1, -1, 0, 0, 0, 1, 1, 1},
      {-2, -2, -2, -1, -1, -1, 0, 0, 0}};
  int b[9][9] = {
      {0, 1, 2, 0, 1, 2, 0, 1, 2},       {0, 1, 2, 0, 1, 2, 0, 1, 2},
      {0, 1, 2, 0, 1, 2, 0, 1, 2},       {0, 1, -1, 0, 1, -1, 0, 1, -1},
      {0, 1, -1, 0, 1, -1, 0, 1, -1},    {0, 1, -1, 0, 1, -1, 0, 1, -1},
      {0, -2, -1, 0, -2, -1, 0, -2, -1}, {0, -2, -1, 0, -2, -1, 0, -2, -1},
      {0, -2, -1, 0, -2, -1, 0, -2, -1}};
  cin >> n >> m;
  int c = 0, p, q;
  memset(vis, 0, sizeof(vis));
  for (int i = 1; i <= m; i++) {
    cin >> p >> q;
    vis[p][q] = 1;
    if (func(p, q, a, b)) {
      if (c != 0)
        c = min(i, c);
      else {
        c = i;
      }
    }
  }
  if (c == 0) {
    cout << "-1\n";
  } else {
    cout << c << "\n";
  }
  return 0;
}
