#include <bits/stdc++.h>
using namespace std;
void judge() {}
const int N = 1e5 + 1;
vector<int> g[N];
int n, q;
int par[N][22];
int h[N];
void dfs(int u = 1, int p = 0) {
  par[u][0] = p;
  h[u] = h[p] + 1;
  for (auto i : g[u]) {
    if (i == p) continue;
    dfs(i, u);
  }
}
void pre() {
  for (int i = 1; i < 22; i++) {
    for (int j = 1; j <= n; j++) {
      if (par[j][i - 1]) {
        par[j][i] = par[par[j][i - 1]][i - 1];
      }
    }
  }
}
int lca(int a, int b) {
  if (h[b] < h[a]) swap(a, b);
  int dis = h[b] - h[a];
  for (int i = 21; i >= 0; i--) {
    if ((dis >> i) & 1) b = par[b][i];
  }
  if (a == b) return a;
  for (int i = 21; i >= 0; i--) {
    if (par[a][i] && par[b][i] && par[a][i] != par[b][i])
      a = par[a][i], b = par[b][i];
  }
  return par[a][0];
}
int dist(int a, int b) { return h[a] + h[b] - 2 * h[lca(a, b)]; }
int query(int a, int b, int c) {
  if (h[a] < h[b]) swap(a, b);
  int la = lca(a, c);
  int lb = lca(b, c);
  if (la == a) {
    return dist(a, c);
  }
  if (lb == b) {
    return min({dist(la, c), dist(lb, c)});
  }
  if (la == lb) return dist(lca(a, b), c);
  if (la == c) {
    if (lb == c)
      return dist(b, c);
    else
      return 0;
  }
  return min({dist(la, c), dist(lb, c), dist(a, c), dist(b, c)});
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie();
  cout.tie();
  ;
  long long t = 1;
  judge();
  while (t--) {
    cin >> n >> q;
    for (int i = 2; i <= n; i++) {
      int a;
      cin >> a;
      g[a].push_back(i);
      g[i].push_back(a);
    }
    dfs();
    pre();
    while (q--) {
      int a, b, c;
      cin >> a >> b >> c;
      int arr[3][3] = {{a, b, c}, {c, a, b}, {c, b, a}};
      int ans = 0;
      for (int i = 0; i < 3; i++) {
        ans = max(ans, query(arr[i][0], arr[i][1], arr[i][2]));
      }
      cout << ans + 1 << '\n';
    }
  }
}
