#include <bits/stdc++.h>
using namespace std;
long long n, q, tp;
vector<long long> g[500001];
long long up[500001][(32)], l;
long long tin[500001], tout[500001], timer;
long long h[500001];
void dfs(long long v, long long p = (1)) {
  h[v] = h[p] + 1;
  up[v][0] = p;
  tin[v] = ++timer;
  for (register long long i = 1; i <= l; ++i)
    up[v][i] = up[up[v][i - 1]][i - 1];
  for (long long to : g[v])
    if (to != p) dfs(to, v);
  tout[v] = ++timer;
}
bool upper(long long a, long long b) {
  return tin[a] <= tin[b] && tout[a] >= tout[b];
}
long long lca(long long a, long long b) {
  if (upper(a, b)) return a;
  if (upper(b, a)) return b;
  for (register long long i = l; i >= 0; --i)
    if (!upper(up[a][i], b)) a = up[a][i];
  return up[a][0];
}
long long dist(long long a, long long b) {
  long long c = lca(a, b);
  return (h[a] - h[c]) + (h[b] - h[c]) + 1;
}
long long test(long long a, long long b, long long c) {
  return (dist(a, c) + dist(b, c) - dist(a, b) + 1) >> 1;
}
long long a, b, c;
int main(void) {
  scanf("%I64d %I64d", &n, &q);
  for (register long long i = 1; i < n; ++i)
    scanf("%I64d", &tp), g[tp].push_back(i + 1), g[i + 1].push_back(tp);
  l = 1;
  while ((1ll << l) <= n) ++l;
  dfs(1);
  while (q--) {
    scanf("%I64d %I64d %I64d", &a, &b, &c);
    printf("%I64d\n", max(test(a, b, c), max(test(a, c, b), test(b, c, a))));
  }
  return 0;
}
