#include <bits/stdc++.h>
using namespace std;
int head[100005], k;
int dep[100005];
int fa[100005][21];
struct Node {
  int v, next;
} e[100005 << 1];
int ans, N, Q;
void Add(int u, int v) { e[k].v = v, e[k].next = head[u], head[u] = k++; }
void BFS() {
  queue<int> One;
  dep[1] = 1;
  One.push(1);
  while (!One.empty()) {
    int u = One.front();
    One.pop();
    for (int i = head[u]; i != -1; i = e[i].next) {
      int v = e[i].v;
      if (dep[v]) continue;
      dep[v] = dep[u] + 1;
      fa[v][0] = u;
      One.push(v);
    }
  }
}
void Get_dp() {
  int i, j;
  for (j = 1; (1 << j) <= N; ++j)
    for (i = 1; i <= N; ++i) fa[i][j] = fa[fa[i][j - 1]][j - 1];
}
int LCA(int u, int v) {
  if (dep[u] < dep[v]) swap(u, v);
  int i;
  for (i = 20; i >= 0; --i)
    if (dep[u] - (1 << i) >= dep[v]) u = fa[u][i];
  if (u == v) return u;
  for (i = 20; i >= 0; --i)
    if (fa[u][i] != fa[v][i]) u = fa[u][i], v = fa[v][i];
  return fa[u][0];
}
int dis(int u, int v) { return dep[u] + dep[v] - 2 * dep[LCA(u, v)]; }
int main() {
  int i, u, v, t;
  while (~scanf("%d%d", &N, &Q)) {
    memset(head, -1, sizeof(head));
    memset(dep, 0, sizeof(dep));
    memset(fa, 0, sizeof(fa));
    k = 0;
    for (i = 2; i <= N; ++i) {
      scanf("%d", &u);
      Add(u, i);
      Add(i, u);
    }
    BFS();
    Get_dp();
    while (Q--) {
      scanf("%d%d%d", &u, &v, &t);
      int ans1 = LCA(u, v);
      int ans2 = LCA(u, t);
      int ans3 = LCA(v, t);
      if (dep[ans2] > dep[ans1]) ans1 = ans2;
      if (dep[ans3] > dep[ans1]) ans1 = ans3;
      int ans = max(dis(ans1, u), max(dis(ans1, v), dis(ans1, t)));
      cout << ans + 1 << endl;
    }
  }
  return 0;
}
