#include <bits/stdc++.h>
using namespace std;
int n, q;
vector<vector<long long int> > par(22, vector<long long int>(100010, -1));
vector<vector<long long int> > edge(100010);
vector<long long int> depth(100010);
void dfs(int src, int currht) {
  depth[src] = currht;
  for (auto it : edge[src]) {
    if (it != src) {
      dfs(it, currht + 1);
      par[0][it] = src;
    }
  }
}
int lca(int u, int v) {
  if (depth[u] < depth[v]) swap(u, v);
  int k = depth[u] - depth[v];
  for (int i = 0; i < 22; i++)
    if ((1 << i) & k) u = par[i][u];
  if (u == v) return u;
  for (int i = 22 - 1; i >= 0; i--)
    if (par[i][u] != par[i][v]) {
      u = par[i][u];
      v = par[i][v];
    }
  return par[0][u];
}
int dis(int u, int v) {
  int w = lca(u, v);
  return depth[u] + depth[v] - 2 * depth[w];
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cin >> n >> q;
  for (int i = 2; i <= n; i++) {
    int tmp;
    cin >> tmp;
    edge[tmp - 1].push_back(i - 1);
  }
  dfs(0, 0);
  for (int k = 1; k < 22; k++) {
    for (int i = 0; i < n; i++) {
      if (par[k - 1][i] == -1) continue;
      par[k][i] = par[k - 1][par[k - 1][i]];
    }
  }
  for (int i = 0; i < q; i++) {
    long long int a, b, c;
    cin >> a >> b >> c;
    --a;
    --b;
    --c;
    long long int l1 = lca(a, b), l2 = lca(a, c), l3 = lca(b, c);
    long long int l;
    if (l1 == l2)
      l = l3;
    else if (l2 == l3)
      l = l1;
    else
      l = l2;
    cout << 1 + max(dis(l, a), max(dis(l, b), dis(l, c))) << endl;
  }
  return 0;
}
