#include <bits/stdc++.h>
using namespace std;
struct RTC {};
typedef vector<vector<int> > graph;
struct LCA_climb {
  vector<int> depth;
  vector<vector<int> > p;
  int n, MAXLOG;
  void init(const graph &g, int root) {
    n = ((int)(g).size());
    depth.assign(n, -1);
    MAXLOG = (31 - __builtin_clz(n - 1)) + 1;
    p = vector<vector<int> >(n, vector<int>(MAXLOG, -1));
    queue<int> q;
    depth[root] = 0;
    q.push(root);
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      for (int i = 1; i < MAXLOG and p[u][i - 1] != -1; i++)
        p[u][i] = p[p[u][i - 1]][i - 1];
      for (int v : g[u])
        if (depth[v] == -1) {
          depth[v] = depth[u] + 1;
          p[v][0] = u;
          q.push(v);
        }
    }
  }
  int climb(int u, int h) {
    h = min(h, depth[u]);
    if (h == 0) return u;
    for (int i = (31 - __builtin_clz(h)); i >= 0 and h > 0; i--)
      if (h >= (1 << i)) u = p[u][i], h -= (1 << i);
    return u;
  }
  int get(int u, int v) {
    if (depth[u] > depth[v]) swap(u, v);
    v = climb(v, depth[v] - depth[u]);
    if (u == v) return u;
    for (int i = (31 - __builtin_clz(depth[u])); i >= 0; i--)
      if (p[u][i] != p[v][i]) u = p[u][i], v = p[v][i];
    return p[u][0];
  }
  int dist(int u, int v) { return depth[u] + depth[v] - 2 * depth[get(u, v)]; }
} lca;
int solve(int a, int b, int t) {
  return (lca.dist(a, t) + lca.dist(b, t) - lca.dist(a, b)) / 2 + 1;
}
int main() {
  int n, q;
  scanf("%d %d", &n, &q);
  graph g(n);
  for (int i = 2; i <= n; i++) {
    int pi;
    scanf("%d", &pi);
    int u = i - 1, v = pi - 1;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  lca.init(g, 0);
  while (q--) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    a--;
    b--;
    c--;
    int op1 = solve(a, b, c);
    int op2 = solve(a, c, b);
    int op3 = solve(b, c, a);
    printf("%d\n", max(op1, max(op2, op3)));
  }
  return 0;
}
