#include <bits/stdc++.h>
using namespace std;
int up[2000001][21];
int tin[200001], tout[200001];
int Time = 0;
int d[200001];
void dfs(vector<int> adj[], int u, int parent) {
  tin[u] = ++Time;
  up[u][0] = parent;
  for (int i = 1; i <= 20; i++) {
    up[u][i] = up[up[u][i - 1]][i - 1];
  }
  for (int i = 0; i < adj[u].size(); i++)
    if (adj[u][i] != parent) {
      d[adj[u][i]] = 1 + d[u];
      dfs(adj, adj[u][i], u);
    }
  tout[u] = ++Time;
}
int is_ancestor(int u, int v) {
  return (tin[u] <= tin[v] && tout[u] >= tout[v]);
}
int lca(int u, int v) {
  if (is_ancestor(u, v)) return u;
  if (is_ancestor(v, u)) return v;
  for (int i = 20; i >= 0; i--)
    if (!is_ancestor(up[u][i], v)) u = up[u][i];
  return up[u][0];
}
int main() {
  int n, q;
  cin >> n >> q;
  vector<int> adj[n + 1];
  int u, v;
  for (int i = 2; i <= n; i++) {
    cin >> u;
    adj[u].push_back(i);
    adj[i].push_back(u);
  }
  d[1] = 0;
  dfs(adj, 1, 1);
  int a[3];
  int s, t, f, lc1, lc2, lc3, ans, mx = 0;
  while (q--) {
    cin >> a[0] >> a[1] >> a[2];
    mx = 0;
    bool p = 1;
    sort(a, a + 3);
    while (p) {
      s = a[0];
      t = a[1];
      f = a[2];
      lc1 = lca(s, f);
      lc2 = lca(s, t);
      lc3 = lca(f, t);
      if (!is_ancestor(lc1, t)) {
        ans = d[lc1] + d[f] - 2 * d[lc1] + 1;
      } else {
        if (lc2 == lc1) {
          if (is_ancestor(f, t))
            ans = 1;
          else if (is_ancestor(t, f)) {
            ans = d[t] + d[f] - 2 * d[t] + 1;
          } else {
            ans = d[f] + d[lc3] - 2 * d[lc3] + 1;
          }
        } else {
          ans = d[lc2] + d[f] - 2 * d[lc1] + 1;
        }
      }
      p = next_permutation(a, a + 3);
      mx = max(mx, ans);
    }
    cout << mx << endl;
  }
  return 0;
}
