#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 7;
const int MOD = 1e9 + 7;
const int INF = (~(1 << 31));
inline int read() {
  long long int x = 0, f = 1;
  char ch = getchar();
  for (; ch < '0' || '9' < ch; ch = getchar())
    if (ch == '-') f = -1;
  for (; '0' <= ch && ch <= '9'; ch = getchar())
    x = (x << 3) + (x << 1) + ch - '0';
  return x * f;
}
int n, m, k;
vector<int> G[N];
void add(int u, int v) {
  G[u].push_back(v);
  G[v].push_back(u);
}
int fa[N][20], dep[N], mi[N][20];
void dfs(int u, int f = 0, int d = 1) {
  fa[u][0] = f;
  dep[u] = d;
  for (int i = 1; i < 20; i++) fa[u][i] = fa[fa[u][i - 1]][i - 1];
  int gz = G[u].size();
  for (int i = 0, to; i < gz; i++) {
    to = G[u][i];
    if (to == f) continue;
    dfs(to, u, d + 1);
  }
}
int LCA(int u, int v) {
  if (dep[u] < dep[v]) swap(u, v);
  int lca = -1, tu = u, tv = v;
  for (int i = 19; i >= 0; i--)
    if (dep[fa[u][i]] >= dep[v]) u = fa[u][i];
  if (u == v)
    lca = v;
  else {
    for (int i = 19; i >= 0; i--) {
      if (fa[u][i] == fa[v][i]) continue;
      u = fa[u][i];
      v = fa[v][i];
    }
    lca = fa[v][0];
  }
  return lca;
}
int solve(int f, int s, int t) {
  int lst = LCA(s, t);
  int lft = LCA(f, t);
  int lfs = LCA(s, f);
  int w;
  if (dep[lst] > dep[lft])
    w = lst;
  else
    w = lft;
  if (dep[lfs] > dep[w]) w = lfs;
  int lca = LCA(f, w);
  return ((dep[f] - dep[lca]) > 0 ? (dep[f] - dep[lca])
                                  : -(dep[f] - dep[lca])) +
         ((dep[lca] - dep[w]) > 0 ? (dep[lca] - dep[w])
                                  : -(dep[lca] - dep[w])) +
         1;
}
int main() {
  int n, q, a, b, c;
  scanf("%d %d", &n, &q);
  for (int i = 2, x; i <= n; i++) {
    scanf("%d", &x);
    add(x, i);
  }
  dfs(1);
  for (int i = 1; i <= q; i++) {
    scanf("%d%d%d", &a, &b, &c);
    printf("%d\n", max(max(solve(a, b, c), solve(b, a, c)), solve(c, a, b)));
  }
  return 0;
}
