#include <bits/stdc++.h>
using namespace std;
const int DEPTH = 17;
vector<int> V[100005];
int fa[DEPTH][100005], dep[100005];
void dfs(int u, int pre) {
  for (int i = 0; i < V[u].size(); i++) {
    int v = V[u][i];
    if (v == pre) continue;
    fa[0][v] = u;
    dep[v] = dep[u] + 1;
    dfs(v, u);
  }
}
int lca(int u, int v) {
  if (dep[u] < dep[v]) {
    swap(u, v);
  }
  int detdep = dep[u] - dep[v];
  for (int i = 0; i < DEPTH; i++) {
    if ((1 << i) & detdep) {
      u = fa[i][u];
    }
  }
  for (int i = DEPTH - 1; i >= 0; i--) {
    if (fa[i][u] != fa[i][v]) {
      u = fa[i][u];
      v = fa[i][v];
    }
  }
  return u == v ? u : fa[0][u];
}
int query(int x, int y, int z) {
  int lxy = dep[x] + dep[y] - 2 * dep[lca(x, y)] + 1;
  int lxz = dep[x] + dep[z] - 2 * dep[lca(x, z)] + 1;
  int lyz = dep[y] + dep[z] - 2 * dep[lca(y, z)] + 1;
  return (max(lxy + lxz - lyz, max(lxy + lyz - lxz, lxz + lyz - lxy)) + 1) / 2;
}
int main() {
  int n, q;
  while (~scanf("%d%d", &n, &q)) {
    for (int i = 1; i <= n; i++) V[i].clear();
    fa[0][1] = 1;
    dep[1] = 0;
    for (int i = 2; i <= n; i++) {
      int tmp;
      scanf("%d", &tmp);
      V[tmp].push_back(i);
      V[i].push_back(tmp);
    }
    dfs(1, 0);
    for (int i = 1; i < DEPTH; i++) {
      for (int j = 1; j <= n; j++) {
        fa[i][j] = fa[i - 1][fa[i - 1][j]];
      }
    }
    while (q--) {
      int x, y, z;
      scanf("%d%d%d", &x, &y, &z);
      printf("%d\n", query(x, y, z));
    }
  }
  return 0;
}
