#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
const ll MOD = 1e9 + 7, N = 1e5 + 10;
const ll LG = log2(N) + 1;
struct LCA {
  vector<vector<ll>> G, parent;
  vector<ll> tin, tout, depth;
  ll timer;
  ll n;
  ll LG;
  LCA(vector<vector<ll>> G, ll R = 1) {
    this->G = G;
    this->n = G.size();
    this->LG = log2(n) + 1;
    this->timer = 0;
    tin.assign(n, 0);
    tout.assign(n, 0);
    depth.assign(n, 0);
    tin[0] = ++timer;
    parent = vector<vector<ll>>(n, vector<ll>(LG));
    dfs(R);
    tout[0] = ++timer;
    compute();
  }
  void dfs(ll u, ll p = 0, ll d = 0) {
    parent[u][0] = p;
    tin[u] = ++timer;
    depth[u] = d;
    for (auto &v : G[u]) {
      if (v != p) {
        dfs(v, u, d + 1);
      }
    }
    tout[u] = ++timer;
  }
  void compute() {
    for (ll j = 1; j < LG; j++) {
      for (ll i = 1; i < n; i++) {
        if (parent[i][j - 1]) {
          parent[i][j] = parent[parent[i][j - 1]][j - 1];
        }
      }
    }
  }
  bool is_anc(ll u, ll v) { return tin[u] <= tin[v] and tout[v] <= tout[u]; }
  ll lca(ll u, ll v) {
    if (is_anc(u, v)) return u;
    if (is_anc(v, u)) return v;
    for (ll i = LG - 1; i >= 0; i--) {
      if (!is_anc(parent[u][i], v)) {
        u = parent[u][i];
      }
    }
    u = parent[u][0];
    return u;
  }
  ll dist(ll u, ll v) { return depth[u] + depth[v] - 2 * depth[lca(u, v)]; }
};
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, q;
  cin >> n >> q;
  vector<vector<ll>> G(n + 1);
  for (ll i = 2; i < n + 1; i++) {
    ll p;
    cin >> p;
    G[i].push_back(p);
    G[p].push_back(i);
  }
  LCA lc(G);
  while (q--) {
    ll a, b, c;
    cin >> a >> b >> c;
    vector<ll> A = {a, b, c};
    sort(A.begin(), A.end());
    ll ans = 0;
    ll cn = 0;
    do {
      cn++;
      ll s = A[0], f = A[1], t = A[2];
      if (lc.lca(s, t) == lc.lca(f, t)) {
        if (lc.depth[lc.lca(s, t)] <= lc.depth[t]) {
          ans = max(ans, lc.depth[t] + lc.depth[lc.lca(s, f)] -
                             2 * lc.depth[lc.lca(s, t)]);
        }
      } else {
        if (lc.depth[lc.lca(s, t)] < lc.depth[t] and
            lc.depth[lc.lca(f, t)] < lc.depth[t]) {
          ans = max(ans, lc.depth[t] - max(lc.depth[lc.lca(s, t)],
                                           lc.depth[lc.lca(f, t)]));
        }
      }
    } while (next_permutation(A.begin(), A.end()));
    cout << ans + 1 << '\n';
  }
  return 0;
}
