#include <bits/stdc++.h>
using namespace std;
template <class X, class Y>
void amax(X& x, const Y& y) {
  if (x < y) x = y;
}
template <class X, class Y>
void amin(X& x, const Y& y) {
  if (x > y) x = y;
}
const int INF = 1e9 + 10;
const long long INFL = 1e18 + 10;
const int MAX = 1e5 + 10;
const int MAXL = 22;
int n, q;
vector<int> es[MAX];
int lca[MAXL][MAX], h[MAX], p[MAX];
int cnt, maxh, limit;
void dfs(int u) {
  h[u] = h[p[u]] + 1;
  amax(maxh, h[u]);
  for (auto v : es[u])
    if (v != p[u]) {
      p[v] = u;
      dfs(v);
    }
}
void build_lca() {
  dfs(1);
  limit = log2(maxh) + 1;
  for (int i = 1; i <= n; i++) lca[0][i] = p[i];
  for (int i = 1; i <= limit; i++)
    for (int j = 1; j <= n; j++) lca[i][j] = lca[i - 1][lca[i - 1][j]];
}
int get_lca(int u, int v) {
  if (h[u] > h[v]) swap(u, v);
  for (int i = limit; i >= 0; i--)
    if (h[lca[i][v]] >= h[u]) v = lca[i][v];
  if (u == v) return u;
  for (int i = limit; i >= 0; i--)
    if (lca[i][v] != lca[i][u]) {
      u = lca[i][u];
      v = lca[i][v];
    }
  return p[u];
}
int get_len(int u, int v) {
  int x = get_lca(u, v);
  return h[u] + h[v] - 2 * h[x];
}
void process() {
  cin >> n >> q;
  for (int i = 2; i <= n; i++) {
    int v;
    cin >> v;
    es[v].push_back(i);
    es[i].push_back(v);
  }
  build_lca();
  while (q--) {
    int a[3];
    for (int i = 0; i < 3; i++) cin >> a[i];
    int r = get_lca(get_lca(a[0], a[1]), a[2]);
    for (int i = 0; i < 3; i++)
      if (r == a[i]) {
        swap(a[0], a[i]);
        break;
      }
    if (r == a[0]) {
      r = get_lca(a[1], a[2]);
    } else {
      for (int i = 0; i < 3; i++)
        for (int j = i + 1; j < 3; j++) {
          int t = get_lca(a[i], a[j]);
          if (get_lca(t, a[3 - i - j]) != t) {
            r = t;
            break;
          }
        }
    }
    int res = get_len(r, a[0]);
    for (int i = 1; i < 3; i++) amax(res, get_len(r, a[i]));
    cout << res + 1 << '\n';
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  process();
}
