#include <bits/stdc++.h>
struct edge {
  int to, next;
} e[200010];
int n, tot, h[100010], dep[100010], fa[100010][21];
void add(int a, int b) {
  tot++;
  e[tot].to = b;
  e[tot].next = h[a];
  h[a] = tot;
}
void dfs(int f, int x) {
  fa[x][0] = f;
  dep[x] = dep[f] + 1;
  for (int i = h[x]; i; i = e[i].next) {
    if (e[i].to != f) dfs(x, e[i].to);
  }
}
void swap(int& a, int& b) { a ^= b ^= a ^= b; }
int lca(int x, int y) {
  if (dep[x] < dep[y]) swap(x, y);
  int i;
  for (i = 20; i >= 0; i--) {
    if (dep[fa[x][i]] >= dep[y]) x = fa[x][i];
  }
  if (x == y) return x;
  for (i = 20; i >= 0; i--) {
    if (fa[x][i] != fa[y][i]) {
      x = fa[x][i];
      y = fa[y][i];
    }
  }
  return fa[x][0];
}
int max(int a, int b) { return a > b ? a : b; }
int calc(int f, int s, int t) {
  int fs, ft, st, fst;
  fs = lca(f, s);
  ft = lca(f, t);
  st = lca(s, t);
  fst = lca(fs, ft);
  if (fs == ft) return dep[f] + dep[st] - 2 * dep[fst] + 1;
  if (fs == st) return dep[f] - dep[ft] + 1;
  return dep[f] - dep[fs] + 1;
}
int main() {
  int q, i, j, a, b, c;
  scanf("%d%d", &n, &q);
  for (i = 2; i <= n; i++) {
    scanf("%d", &a);
    add(a, i);
    add(i, a);
  }
  dep[0] = -1;
  dfs(0, 1);
  for (j = 1; j < 21; j++) {
    for (i = 1; i <= n; i++) fa[i][j] = fa[fa[i][j - 1]][j - 1];
  }
  while (q--) {
    scanf("%d%d%d", &a, &b, &c);
    printf("%d\n", max(max(calc(a, b, c), calc(b, a, c)), calc(c, a, b)));
  }
}
