#include <bits/stdc++.h>
using namespace std;
struct node {
  int y, next;
} e[100010 * 2];
int n, q, len, Link[100010], deep[100010], f[100010], anc[100010][25];
void insert(int x, int y) {
  e[++len].next = Link[x];
  Link[x] = len;
  e[len].y = y;
}
void dfs(int x, int fa) {
  anc[x][0] = f[x];
  for (int i = 1; i <= 20; i++) anc[x][i] = anc[anc[x][i - 1]][i - 1];
  for (int i = Link[x]; i; i = e[i].next)
    if (e[i].y != fa) {
      f[e[i].y] = x;
      deep[e[i].y] = deep[x] + 1;
      dfs(e[i].y, x);
    }
}
int lca(int x, int y) {
  if (deep[x] < deep[y]) swap(x, y);
  for (int i = 20; i >= 0; i--)
    if (deep[anc[x][i]] >= deep[y]) x = anc[x][i];
  if (x == y) return x;
  for (int i = 20; i >= 0; i--)
    if (anc[x][i] != anc[y][i]) x = anc[x][i], y = anc[y][i];
  return f[x];
}
int cal(int a, int b, int c) {
  if (b >= a && c >= a)
    return min(b, c) - a + 1;
  else if (b <= a && c <= a)
    return a - max(b, c) + 1;
  else
    return 1;
}
int solve(int a, int b, int c) {
  int ab = lca(a, b);
  int ac = lca(a, c);
  int bc = lca(b, c);
  if (ab == a && ac == a) {
    return deep[bc] - deep[a] + 1;
  } else if (ab == a && ac != a || ab != a && ac == a)
    return 1;
  else if (ab != a && ac != a) {
    if (ac == ab) {
      return solve(ac, b, c) + deep[a] - deep[ac];
    } else {
      return deep[a] - max(deep[ac], deep[ab]) + 1;
    }
  }
}
int main() {
  cin >> n >> q;
  for (int i = 2; i <= n; i++) {
    int x;
    cin >> x;
    insert(i, x);
    insert(x, i);
  }
  deep[1] = 1;
  dfs(1, 0);
  while (q--) {
    int a, b, c;
    cin >> a >> b >> c;
    int res = max(solve(a, b, c), solve(b, a, c));
    res = max(res, solve(c, a, b));
    printf("%d\n", res);
  }
  return 0;
}
