#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 1;
const int LOG = 20;
vector<int> graph[MAXN];
int dp[MAXN][LOG + 1];
int tin[MAXN];
int tout[MAXN];
int h[MAXN];
int timer = 0;
void dfs(int v, int p) {
  tin[v] = timer++;
  dp[v][0] = p;
  for (int i = 1; i < LOG; i++) dp[v][i] = dp[dp[v][i - 1]][i - 1];
  for (auto u : graph[v]) {
    if (u == p) continue;
    h[u] = h[v] + 1;
    dfs(u, v);
  }
  tout[v] = timer++;
}
bool is_ancestor(int v, int u) {
  return (tin[v] <= tin[u]) && (tout[v] >= tout[u]);
}
int lca(int v, int u) {
  if (is_ancestor(v, u)) return v;
  if (is_ancestor(u, v)) return u;
  for (int i = LOG; i >= 0; i--) {
    if (!is_ancestor(dp[v][i], u)) v = dp[v][i];
  }
  return dp[v][0];
}
int root_lca(int v, int u, int root) {
  return lca(v, root) ^ lca(u, root) ^ lca(v, u);
}
int check(int s, int f, int t) {
  int l = root_lca(s, t, f);
  int f_l = lca(f, l);
  return h[l] + h[f] - 2 * h[f_l];
}
signed main() {
  int n, q;
  cin >> n >> q;
  for (int i = 1; i < n; i++) {
    int p;
    cin >> p;
    p--;
    graph[p].push_back(i);
    graph[i].push_back(p);
  }
  dfs(0, 0);
  for (int i = 0; i < q; i++) {
    vector<int> cur(3);
    cin >> cur[0] >> cur[1] >> cur[2];
    cur[0]--;
    cur[1]--;
    cur[2]--;
    int ans_am = 0;
    for (int s = 0; s < 3; s++) {
      for (int f = 0; f < 3; f++) {
        if (f == s) continue;
        int t = (3 ^ s) ^ f;
        if (f == t || s == t) continue;
        int cur_ans = check(cur[s], cur[f], cur[t]);
        if (ans_am < cur_ans) {
          ans_am = cur_ans;
        }
      }
    }
    cout << ans_am + 1 << '\n';
  }
  return 0;
}
