#include <bits/stdc++.h>
const int maxn = 1e5 + 10;
using namespace std;
int head[maxn], deep[maxn], k;
int f[maxn][23];
int n, q, root;
struct edge {
  int v, next;
} e[maxn << 1];
void adde(int u, int v) {
  e[k].v = v;
  e[k].next = head[u];
  head[u] = k++;
}
void init() {
  k = 0;
  memset(head, -1, sizeof(head));
  memset(f, 0, sizeof(f));
  memset(deep, -1, sizeof(deep));
}
void dfs(int u, int fa) {
  f[u][0] = fa;
  deep[u] = deep[fa] + 1;
  for (int j = 1; j <= 20; j++) f[u][j] = f[f[u][j - 1]][j - 1];
  for (int i = head[u]; i != -1; i = e[i].next) {
    int v = e[i].v;
    if (v == fa) continue;
    dfs(v, u);
  }
}
int lca(int u, int v) {
  if (deep[u] > deep[v]) swap(u, v);
  for (int j = 20; j >= 0; j--) {
    if (deep[v] - (1 << j) >= deep[u]) v = f[v][j];
  }
  if (u == v) return u;
  for (int j = 20; j >= 0; j--) {
    if (f[u][j] == f[v][j]) continue;
    u = f[u][j];
    v = f[v][j];
  }
  return f[u][0];
}
int dis(int u, int v) {
  int ans = deep[u] + deep[v] - 2 * deep[lca(u, v)];
  return ans;
}
int main() {
  init();
  scanf("%d%d", &n, &q);
  int x;
  for (int i = 2; i <= n; i++) {
    scanf("%d", &x);
    adde(x, i);
    adde(i, x);
  }
  dfs(1, 0);
  int u1, u2, u3;
  while (q--) {
    scanf("%d%d%d", &u1, &u2, &u3);
    int lca1 = lca(u1, u2);
    int lca2 = lca(u1, u3);
    int lca3 = lca(u2, u3);
    if (deep[lca3] > deep[lca1]) lca1 = lca3;
    if (deep[lca2] > deep[lca1]) lca1 = lca2;
    int ans = max(dis(lca1, u3), max(dis(lca1, u2), dis(lca1, u1))) + 1;
    printf("%d\n", ans);
  }
  return 0;
}
