#include <bits/stdc++.h>
using namespace std;
int n, q;
vector<int> g[100000 + 5];
int p[100000 + 5], level[100000 + 5], dp[100000 + 5][20];
int lca(int u, int v) {
  if (level[u] < level[v]) swap(u, v);
  int i, l;
  l = ceil(log2(level[u]));
  for (i = l; i >= 0; i--) {
    if ((level[u] - (1 << i)) >= level[v]) u = dp[u][i];
  }
  if (u == v) return u;
  for (i = l; i >= 0; i--) {
    if (dp[u][i] != -1 && dp[u][i] != dp[v][i]) {
      u = dp[u][i];
      v = dp[v][i];
    }
  }
  return dp[u][0];
}
void dfs(int u, int x, int l) {
  int i, v;
  level[u] = l;
  for (i = 0; i < (int)g[u].size(); i++) {
    v = g[u][i];
    if (v != x) dfs(v, u, l + 1);
  }
}
int compute(int x, int y, int z) {
  int u, v, f, ans = 0;
  u = lca(x, y);
  v = lca(x, z);
  if (u == x) {
    if (v == x) {
      u = lca(y, z);
      if (u == x)
        ans = max(ans, 1);
      else
        ans = max(ans, (level[u] - level[x] + 1));
    } else
      ans = max(ans, 1);
  } else if (u == y) {
    if (v == x)
      ans = max(ans, 1);
    else {
      u = lca(v, y);
      if (u == y)
        ans = max(ans, (level[x] - level[v] + 1));
      else
        ans = max(ans, (level[x] - level[y] + 1));
    }
  } else {
    if (v == x)
      ans = max(ans, 1);
    else {
      f = lca(v, u);
      if (f == u) {
        f = lca(y, z);
        if (f == u)
          ans = max(ans, (level[x] - level[v] + 1));
        else
          ans = max(ans, (level[x] - level[u] + level[f] - level[u] + 1));
      } else
        ans = max(ans, (level[x] - level[u] + 1));
    }
  }
  return ans;
}
int main() {
  int i, j, ans, x, y, z;
  scanf("%d%d", &n, &q);
  memset(dp, -1, sizeof(dp));
  for (i = 2; i <= n; i++) {
    scanf("%d", &p[i]);
    g[p[i]].push_back(i);
    g[i].push_back(p[i]);
    dp[i][0] = p[i];
  }
  dfs(1, -1, 0);
  for (j = 1; (1 << j) <= n; j++)
    for (i = 1; i <= n; i++)
      if (dp[i][j - 1] != -1) dp[i][j] = dp[dp[i][j - 1]][j - 1];
  while (q--) {
    scanf("%d%d%d", &x, &y, &z);
    ans = 0;
    ans = max(ans, compute(x, y, z));
    ans = max(ans, compute(y, x, z));
    ans = max(ans, compute(z, x, y));
    printf("%d\n", ans);
  }
  return 0;
}
