#include <bits/stdc++.h>
using namespace std;
vector<int> v[100005];
int level[100005], parent[100005][17];
bool vis[100005];
void dfs(int u) {
  int i;
  for (i = 0; i < v[u].size(); i++) {
    if (!vis[v[u][i]]) {
      vis[v[u][i]] = true;
      level[v[u][i]] = level[u] + 1;
      parent[v[u][i]][0] = u;
      dfs(v[u][i]);
    }
  }
}
int query(int p, int q) {
  if (level[p] < level[q]) swap(p, q);
  int j;
  for (j = 16; j >= 0; j--) {
    if (parent[p][j] != -1 && level[parent[p][j]] >= level[q]) p = parent[p][j];
  }
  if (p == q) return p;
  for (j = 16; j >= 0; j--) {
    if (parent[p][j] != -1 && parent[q][j] != -1 &&
        parent[p][j] != parent[q][j]) {
      p = parent[p][j];
      q = parent[q][j];
    }
  }
  return parent[p][0];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, q, i, j, x, a, b, c, lca, ab, bc, ac;
  cin >> n >> q;
  for (i = 2; i <= n; i++) {
    cin >> x;
    v[i].push_back(x);
    v[x].push_back(i);
  }
  memset(vis, false, sizeof(vis));
  memset(parent, -1, sizeof(parent));
  level[1] = 0;
  vis[1] = true;
  dfs(1);
  for (j = 1; (1 << j) < n; j++) {
    for (i = 1; i <= n; i++) {
      if (parent[i][j - 1] != -1 && parent[parent[i][j - 1]][j - 1] != -1)
        parent[i][j] = parent[parent[i][j - 1]][j - 1];
    }
  }
  while (q--) {
    cin >> a >> b >> c;
    ab = query(a, b);
    bc = query(b, c);
    ac = query(a, c);
    if (level[ab] >= level[bc] && level[ab] >= level[ac])
      lca = ab;
    else if (level[bc] >= level[ab] && level[bc] >= level[ac])
      lca = bc;
    else
      lca = ac;
    ab = query(lca, a);
    ab = level[lca] + level[a] - (2 * level[ab]);
    bc = query(lca, b);
    bc = level[lca] + level[b] - (2 * level[bc]);
    ac = query(lca, c);
    ac = level[lca] + level[c] - (2 * level[ac]);
    cout << max(ab, max(bc, ac)) + 1 << endl;
  }
  return 0;
}
