#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int> > v(n + 1, vector<int>(n + 1, 0));
  int x, y;
  bool ck = false;
  int res = 0;
  for (int move = 1; move <= m; ++move) {
    cin >> x >> y;
    if (ck == true) continue;
    v[x][y] = 1;
    int count = 0;
    for (int i = x; i < x + 3 && i > 0 && i <= n; ++i)
      for (int j = y; j < y + 3 && j > 0 && j <= n; ++j)
        if (v[i][j] == 1) count++;
    if (count == 9) ck = true;
    count = 0;
    for (int i = x; i < x + 3 && i > 0 && i <= n; ++i)
      for (int j = y - 1; j < y + 2 && j > 0 && j <= n; ++j)
        if (v[i][j] == 1) count++;
    if (count == 9) ck = true;
    count = 0;
    for (int i = x; i < x + 3 && i > 0 && i <= n; ++i)
      for (int j = y - 2; j < y + 1 && j > 0 && j <= n; ++j)
        if (v[i][j] == 1) count++;
    if (count == 9) ck = true;
    count = 0;
    for (int i = x - 1; i < x + 2 && i > 0 && i <= n; ++i)
      for (int j = y; j < y + 3 && j > 0 && j <= n; ++j)
        if (v[i][j] == 1) count++;
    if (count == 9) ck = true;
    count = 0;
    for (int i = x - 1; i < x + 2 && i > 0 && i <= n; ++i)
      for (int j = y - 1; j < y + 2 && j > 0 && j <= n; ++j)
        if (v[i][j] == 1) count++;
    if (count == 9) ck = true;
    count = 0;
    for (int i = x - 1; i < x + 2 && i > 0 && i <= n; ++i)
      for (int j = y - 2; j < y + 1 && j > 0 && j <= n; ++j)
        if (v[i][j] == 1) count++;
    if (count == 9) ck = true;
    count = 0;
    for (int i = x - 2; i < x + 1 && i > 0 && i <= n; ++i)
      for (int j = y; j < y + 3 && j > 0 && j <= n; ++j)
        if (v[i][j] == 1) count++;
    if (count == 9) ck = true;
    count = 0;
    for (int i = x - 2; i < x + 1 && i > 0 && i <= n; ++i)
      for (int j = y - 1; j < y + 2 && j > 0 && j <= n; ++j)
        if (v[i][j] == 1) count++;
    if (count == 9) ck = true;
    count = 0;
    for (int i = x - 2; i < x + 1 && i > 0 && i <= n; ++i)
      for (int j = y - 2; j < y + 1 && j > 0 && j <= n; ++j)
        if (v[i][j] == 1) count++;
    if (count == 9) ck = true;
    if (ck == true) res = move;
  }
  if (ck == true)
    cout << res << endl;
  else
    cout << "-1" << endl;
  return 0;
}
