#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > g(100007);
int used[100007], p[100007], d[100007], dp[100007][20], n;
int dfs(int x, int dx) {
  used[x] = 1;
  for (int i = 0; i < g[x].size(); i++)
    if (used[g[x][i]] == 0) {
      d[g[x][i]] = dx + 1;
      dfs(g[x][i], dx + 1);
    }
  return 0;
}
void preprocessing() {
  dfs(1, 0);
  for (int i = 1; i <= n; i++) dp[i][0] = p[i];
  for (int j = 1; j <= log2(n); j++)
    for (int i = 1; i <= n; i++) dp[i][j] = dp[dp[i][j - 1]][j - 1];
}
int LCA(int u, int v) {
  if (d[u] < d[v]) swap(u, v);
  for (int k = log2(n); k >= 0; k--)
    if (d[dp[u][k]] - d[v] >= 0) u = dp[u][k];
  if (u == v) return u;
  for (int k = log2(n); k >= 0; k--)
    if (dp[u][k] != dp[v][k]) {
      u = dp[u][k];
      v = dp[v][k];
    }
  return p[u];
}
int r(int u, int v) {
  int lca = LCA(u, v);
  return abs(d[u] - d[lca]) + abs(d[v] - d[lca]);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int i, u, v, x, q, a, b, c, maxx, x1, x2, x3, cross;
  vector<int> ans;
  cin >> n >> q;
  for (i = 2; i <= n; i++) {
    cin >> x;
    g[i].push_back(x);
    g[x].push_back(i);
    p[i] = x;
  }
  p[1] = 1;
  preprocessing();
  for (i = 1; i <= q; i++) {
    cin >> a >> b >> c;
    maxx = 0;
    x1 = d[LCA(a, b)];
    x2 = d[LCA(b, c)];
    x3 = d[LCA(a, c)];
    if (max({x1, x2, x3}) == x1)
      cross = LCA(a, b);
    else if (max({x1, x2, x3}) == x2)
      cross = LCA(b, c);
    else
      cross = LCA(a, c);
    ans.push_back(max({r(cross, a), r(cross, b), r(cross, c)}) + 1);
  }
  for (i = 0; i <= q - 1; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}
