#include <bits/stdc++.h>
using namespace std;
int n, q, vis[100005], parent[100005], level[100005], table[100005][32];
vector<int> adj[100005];
void bfs(int root) {
  memset(vis, 0, sizeof vis);
  parent[root] = -1;
  vis[root] = 1;
  level[root] = 0;
  queue<int> Q;
  Q.push(root);
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    for (int i = 0; i < adj[u].size(); i++) {
      int v = adj[u][i];
      if (vis[v] == 0) {
        vis[v] = 1;
        parent[v] = u;
        vis[v] = 1;
        level[v] = level[u] + 1;
        Q.push(v);
      }
    }
  }
}
void process() {
  memset(table, -1, sizeof table);
  for (int i = 1; i <= n; i++) table[i][0] = parent[i];
  for (int j = 1; j <= 30; j++) {
    for (int i = 1; i <= n; i++) {
      if (table[i][j - 1] != -1) table[i][j] = table[table[i][j - 1]][j - 1];
    }
  }
}
int findLCA(int u, int v) {
  if (level[u] < level[v]) swap(u, v);
  int dist = level[u] - level[v];
  while (dist > 0) {
    int raise_by = log2(dist);
    u = table[u][raise_by];
    dist -= (1 << raise_by);
  }
  if (u == v) return u;
  for (int j = 30; j >= 0; j--) {
    if (table[u][j] != -1 && table[u][j] != table[v][j]) {
      u = table[u][j];
      v = table[v][j];
    }
  }
  return parent[u];
}
bool onPath(int u, int v) {
  if (level[u] < level[v]) return false;
  int dist = level[u] - level[v];
  while (dist > 0) {
    int raise_by = log2(dist);
    u = table[u][raise_by];
    dist -= (1 << raise_by);
  }
  return (u == v);
}
int dist(int u, int v) {
  if (onPath(u, v) || onPath(v, u)) {
    return abs(level[u] - level[v]) + 1;
  }
  int lca = findLCA(u, v);
  int ans = level[u] - level[lca] + level[v] - level[lca] + 1;
  return ans;
}
int query(int x, int y, int z) {
  int c = findLCA(x, z), d = findLCA(y, z);
  int ans = level[z] - max(level[c], level[d]) + 1;
  if (c != d) return ans;
  int t = findLCA(x, y);
  ans += max(0, level[t] - level[c]);
  return ans;
}
int main() {
  scanf("%d %d", &n, &q);
  for (int i = 2; i <= n; i++) {
    int x;
    scanf("%d", &x);
    adj[i].push_back(x);
    adj[x].push_back(i);
  }
  bfs(1);
  process();
  for (int i = 0; i < q; i++) {
    int u, v, t;
    scanf("%d %d %d", &u, &v, &t);
    int maxs = 0;
    maxs = max(maxs, query(u, v, t));
    maxs = max(maxs, query(v, t, u));
    maxs = max(maxs, query(u, t, v));
    printf("%d\n", maxs);
  }
}
