#include <bits/stdc++.h>
using namespace std;
struct ajacent_list {
  int end, next;
} alis[100010 * 2];
int fir[100010], tot, dep[100010], jump[100010][17], n, q, a, b, c, ans, tem,
    ab, ac, bc;
void build(int u, int v) {
  alis[++tot].next = fir[u];
  fir[u] = tot;
  alis[tot].end = v;
}
int LCA(int a, int b) {
  if (dep[a] < dep[b]) swap(a, b);
  for (int i = 16; i >= 0; i--)
    if (dep[jump[a][i]] >= dep[b]) a = jump[a][i];
  if (a == b) return a;
  for (int i = 16; i >= 0; i--)
    if (jump[a][i] != jump[b][i]) {
      a = jump[a][i];
      b = jump[b][i];
    }
  return jump[a][0];
}
int dis(int u, int v) {
  int lca = LCA(u, v);
  return dep[u] + dep[v] - 2 * dep[lca] + 1;
}
void DFS(int x) {
  for (int u = fir[x]; u; u = alis[u].next)
    if (!dep[alis[u].end]) {
      dep[alis[u].end] = dep[x] + 1;
      jump[alis[u].end][0] = x;
      DFS(alis[u].end);
    }
}
int solve(int a, int b, int c) {
  int ac = LCA(a, c), bc = LCA(b, c);
  if ((ac == c) ^ (bc == c))
    return 1;
  else if ((ac == c) && (bc == c))
    return dis(LCA(a, b), c);
  else if (ac != bc)
    return min(dis(c, ac), dis(c, bc));
  else
    return dis(LCA(a, b), c);
}
int main() {
  scanf("%d%d", &n, &q);
  for (int i = 1; i < n; i++) {
    scanf("%d", &tem);
    build(tem, i + 1);
    build(i + 1, tem);
  }
  dep[1] = 1;
  DFS(1);
  for (int i = 1; i <= 16; i++)
    for (int j = 1; j <= n; j++) jump[j][i] = jump[jump[j][i - 1]][i - 1];
  for (int i = 1; i <= q; i++) {
    ans = 1;
    scanf("%d%d%d", &a, &b, &c);
    ans = max(ans, solve(a, b, c));
    ans = max(ans, solve(a, c, b));
    ans = max(ans, solve(b, c, a));
    printf("%d\n", ans);
  }
  return 0;
}
