#include <bits/stdc++.h>
using namespace std;
const int max_n = 100010;
int parent[20][max_n];
int depth[max_n];
int n, q;
vector<int> G[max_n];
void dfs(int v, int par, int d) {
  depth[v] = d;
  parent[0][v] = par;
  for (auto u : G[v]) {
    if (u != par) {
      dfs(u, v, d + 1);
    }
  }
}
void init() {
  dfs(1, -1, 0);
  for (int k = 0; k < 19; k++) {
    for (int i = 1; i <= n; i++) {
      if (parent[k][i] < 0)
        parent[k + 1][i] = -1;
      else
        parent[k + 1][i] = parent[k][parent[k][i]];
    }
  }
}
int LCA(int u, int v) {
  if (depth[u] > depth[v]) swap(u, v);
  for (int k = 0; k < 20; k++) {
    if ((depth[v] - depth[u]) >> k & 1) {
      v = parent[k][v];
    }
  }
  if (u == v) return u;
  for (int k = 19; k >= 0; k--) {
    if (parent[k][u] != parent[k][v]) {
      u = parent[k][u];
      v = parent[k][v];
    }
  }
  return parent[0][u];
}
int dis(int u, int v) { return depth[u] + depth[v] - 2 * depth[LCA(u, v)]; }
int res(int a, int b, int c) {
  int x = dis(a, b), y = dis(b, c), z = dis(a, c);
  int A = (x + y - z) / 2;
  int B = (x + z - y) / 2;
  int C = (y + z - x) / 2;
  return max(max(A, B), C) + 1;
}
int main() {
  scanf("%d%d", &n, &q);
  for (int i = 2; i <= n; i++) {
    int u;
    scanf("%d", &u);
    G[u].push_back(i);
    G[i].push_back(u);
  }
  init();
  for (int i = 0; i < q; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    printf("%d\n", res(a, b, c));
  }
  return 0;
}
