#include <bits/stdc++.h>
using namespace std;
vector<int> v[100010];
int logg[200010], l, rmq[200010][20], niv[100010], first[100010], vaz[100010],
    v1[6], sol;
void dfs(int nod, int nivel) {
  vaz[nod] = 1;
  rmq[++l][0] = nod;
  niv[nod] = nivel;
  first[nod] = l;
  for (int i = 0; i < v[nod].size(); i++)
    if (vaz[v[nod][i]] == 0) {
      dfs(v[nod][i], nivel + 1);
      rmq[++l][0] = nod;
    }
}
int lca(int a, int b) {
  a = first[a];
  b = first[b];
  if (a > b) swap(a, b);
  int l = logg[b - a + 1];
  if (niv[rmq[a][l]] < niv[rmq[b - (1 << l) + 1][l]])
    return rmq[a][l];
  else
    return rmq[b - (1 << l) + 1][l];
}
int solve(int s, int f, int t) {
  int sol = 0;
  int a = lca(s, f), b = lca(f, t), c = lca(s, t);
  sol = min(niv[f] - niv[a] + 1, niv[f] - niv[b] + 1);
  if (niv[c] >= niv[a]) {
    sol += niv[c] - niv[a];
  }
  return sol;
}
int main() {
  int n, q, a, b, c, x;
  scanf("%d%d", &n, &q);
  for (int i = 2; i <= n; i++) {
    scanf("%d", &x);
    v[x].push_back(i);
  }
  dfs(1, 0);
  logg[1] = 0;
  for (int i = 2; i <= l; i++) logg[i] = logg[i / 2] + 1;
  for (int i = 1; i <= logg[l]; i++)
    for (int j = 1; j + (1 << i) <= l + 1; j++)
      if (niv[rmq[j][i - 1]] <= niv[rmq[j + (1 << (i - 1))][i - 1]])
        rmq[j][i] = rmq[j][i - 1];
      else
        rmq[j][i] = rmq[j + (1 << (i - 1))][i - 1];
  for (int i = 1; i <= q; i++) {
    scanf("%d%d%d", &a, &b, &c);
    sol = 0;
    sol = max(sol, solve(a, b, c));
    sol = max(sol, solve(a, c, b));
    sol = max(sol, solve(b, a, c));
    sol = max(sol, solve(b, c, a));
    sol = max(sol, solve(c, a, b));
    sol = max(sol, solve(c, b, a));
    printf("%d\n", sol);
  }
  return 0;
}
