#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 6;
const int LOGN = 19;
vector<int> adj[N];
int etour[N << 1] = {};
int vst[N] = {}, lv[N] = {};
int ed, logN;
int dp[N << 1][LOGN] = {};
int n, q;
bool euler_tour(int u, int lvl) {
  if (vst[u] != 0) {
    return false;
  }
  lv[u] = lvl;
  vst[u] = ed;
  etour[ed] = u;
  ed++;
  for (auto v : adj[u]) {
    if (euler_tour(v, lvl + 1)) {
      etour[ed] = u;
      ed++;
    }
  }
  return true;
}
void process() {
  ed = 1;
  euler_tour(1, 1);
  ed--;
  logN = log2(ed) + 1;
  for (int i = 1; i <= ed; i++) {
    dp[i][0] = i;
  }
  for (int j = 1; j <= logN; j++) {
    int j2 = 1 << (j - 1);
    for (int i = 1; ((i + (1 << j)) - 1) <= ed; i++) {
      int l = dp[i][j - 1], r = dp[i + j2][j - 1];
      if (lv[etour[l]] < lv[etour[r]]) {
        dp[i][j] = l;
      } else {
        dp[i][j] = r;
      }
    }
  }
}
int lca(int u, int v) {
  u = vst[u];
  v = vst[v];
  if (u > v) swap(u, v);
  int logIJ = log2(v - u + 1);
  int l = etour[dp[u][logIJ]];
  int r = etour[dp[v - (1 << logIJ) + 1][logIJ]];
  if (lv[l] < lv[r]) {
    return l;
  } else {
    return r;
  }
}
int solve(int s, int f, int t) {
  int lca_st = lca(s, t);
  int lca_sf = lca(s, f);
  int lca_ft = lca(f, t);
  int lca_sft = lca(s, lca_ft);
  int ans = 0;
  if (lv[lca_sf] <= lv[lca_st]) {
    ans = lv[lca_st] - lv[lca_sf];
    ans += lv[f] - lv[lca_ft];
    ans += 1;
  } else {
    ans = lv[f] - lv[lca_sf] + 1;
  }
  return ans;
}
int solver(int a, int b, int c) {
  int ans = -1;
  ans = max(ans, solve(a, b, c));
  ans = max(ans, solve(a, c, b));
  ans = max(ans, solve(b, a, c));
  ans = max(ans, solve(b, c, a));
  ans = max(ans, solve(c, a, b));
  ans = max(ans, solve(c, b, a));
  return ans;
}
int main() {
  cin >> n >> q;
  for (int i = 2; i <= n; i++) {
    int p;
    cin >> p;
    adj[i].push_back(p);
    adj[p].push_back(i);
  }
  process();
  while (q--) {
    int a, b, c;
    cin >> a >> b >> c;
    cout << solver(a, b, c) << endl;
  }
  return 0;
}
