#include <bits/stdc++.h>
using namespace std;
vector<int> g[100005];
int depth[100005], par[18][100005];
void dfs(int u, int p, int d = 1) {
  depth[u] = d;
  par[0][u] = p;
  for (int i = 0; i < g[u].size(); i++) {
    int v = g[u][i];
    if (v == p) continue;
    dfs(v, u, d + 1);
  }
}
int LCA(int a, int b) {
  if (depth[a] < depth[b]) swap(a, b);
  int diff = depth[a] - depth[b];
  for (int i = 17; i >= 0; i--)
    if ((diff >> i) & 1) a = par[i][a];
  if (a == b) return a;
  for (int i = 17; i >= 0; i--)
    if (par[i][a] != par[i][b]) a = par[i][a], b = par[i][b];
  return par[0][a];
}
int cal(int s, int t, int f) {
  int lcaST = LCA(s, t), lcaSF = LCA(s, f), lcaTF = LCA(t, f);
  if (depth[lcaSF] < depth[lcaTF]) swap(lcaSF, lcaTF);
  int res = depth[f] - depth[lcaSF];
  if (depth[lcaST] > depth[lcaSF]) res += depth[lcaST] - depth[lcaSF];
  return res + 1;
}
int main() {
  int n, q, p;
  scanf("%d%d", &n, &q);
  for (int i = 2; i <= n; i++)
    scanf("%d", &p), g[p].push_back(i), g[i].push_back(p);
  dfs(1, 0);
  for (int j = 1; j <= 17; j++) {
    for (int i = 1; i <= n; i++)
      if (par[j - 1][i] != 0) par[j][i] = par[j - 1][par[j - 1][i]];
  }
  while (q--) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    printf("%d\n", max(cal(a, b, c), max(cal(b, c, a), cal(a, c, b))));
  }
}
