#include <bits/stdc++.h>
using namespace std;
int fa[200000][17], g[200000], dfn[200000], a[200000 * 2][2], deep[200000],
    r[200000];
int n, t, x, y, z, ans;
void ins(int x, int y) {
  static int sum = 1;
  a[++sum][0] = y, a[sum][1] = g[x], g[x] = sum;
}
void dfs(int x) {
  static int sum = 0;
  dfn[x] = ++sum;
  deep[x]++;
  for (int i = 0; fa[fa[x][i]][i]; i++) fa[x][i + 1] = fa[fa[x][i]][i];
  for (int i = g[x]; i; i = a[i][1])
    if (a[i][0] != fa[x][0])
      fa[a[i][0]][0] = x, deep[a[i][0]] = deep[x], dfs(a[i][0]);
  r[x] = sum;
}
int getlca(int x, int y) {
  if (deep[x] < deep[y]) swap(x, y);
  int i = 16;
  while (deep[x] != deep[y]) {
    for (; deep[fa[x][i]] < deep[y]; i--)
      ;
    x = fa[x][i];
  }
  i = 16;
  while (x != y) {
    for (; i && fa[x][i] == fa[y][i]; i--)
      ;
    x = fa[x][i], y = fa[y][i];
  }
  return x;
}
int dis(int x, int y) {
  int lca = getlca(x, y);
  return deep[x] + deep[y] - 2 * deep[lca] + 1;
}
int work(int x, int y, int z) {
  int lca1 = getlca(x, y), lca2 = getlca(y, z), lca3;
  if (lca1 == y) {
    if (lca2 == y) {
      lca3 = getlca(x, z);
      return deep[lca3] - deep[y] + 1;
    } else
      return 1;
  } else {
    if (lca2 == y)
      return 1;
    else {
      lca1 = getlca(x, y);
      lca2 = getlca(y, z);
      if (deep[lca1] < deep[lca2]) swap(x, y), swap(lca1, lca2);
      lca3 = getlca(x, z);
      if (dfn[lca1] <= dfn[lca3] && dfn[lca3] <= r[lca1])
        return dis(lca3, y);
      else
        return dis(lca1, y);
    }
  }
}
int main() {
  scanf("%d %d", &n, &t);
  for (int i = 2; i <= n; i++) {
    scanf("%d", &x);
    ins(i, x), ins(x, i);
  }
  dfs(1);
  while (t--) {
    scanf("%d %d %d", &x, &y, &z);
    printf(
        "%d\n",
        max(work(x, y, z),
            max(work(y, x, z),
                max(work(x, z, y),
                    max(work(y, z, x), max(work(z, y, x), work(z, x, y)))))));
  }
  return 0;
}
