#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200000 + 100;
const int M = 30;
vector<int> G[MAXN];
int deep[MAXN], fa[MAXN][M];
void DFS(int cur, int pre) {
  fa[cur][0] = pre;
  for (int i = 1; i < 20; ++i) fa[cur][i] = fa[fa[cur][i - 1]][i - 1];
  for (int i = 0; i < G[cur].size(); i++) {
    int to = G[cur][i];
    if (to == pre) continue;
    deep[to] = deep[cur] + 1;
    DFS(to, cur);
  }
}
int LCA(int v, int u) {
  if (deep[v] > deep[u]) swap(v, u);
  for (int i = 0; i < 20; ++i)
    if (deep[u] - deep[v] >> i & 1) u = fa[u][i];
  for (int i = 19; i >= 0; --i)
    if (fa[v][i] != fa[u][i]) v = fa[v][i], u = fa[u][i];
  return v == u ? v : fa[v][0];
}
int cal(int s, int t, int f) {
  int ans = 0, sf = LCA(s, f) == f, tf = LCA(t, f) == f;
  if (sf != tf)
    return 1;
  else if (sf)
    ans = deep[LCA(s, t)] - deep[f];
  else if (LCA(s, f) != LCA(t, f))
    ans = deep[f] - max(deep[LCA(f, s)], deep[LCA(f, t)]);
  else
    ans = deep[LCA(s, t)] + deep[f] - 2 * deep[LCA(s, f)];
  return ans + 1;
}
int main() {
  int n, q;
  scanf("%d%d", &n, &q);
  for (int i = 2; i <= n; ++i) {
    int x;
    scanf("%d", &x);
    G[i].push_back(x);
    G[x].push_back(i);
  }
  DFS(1, 0);
  while (q--) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    printf("%d\n", max(cal(a, b, c), max(cal(b, c, a), cal(c, a, b))));
  }
  return 0;
}
