#include <bits/stdc++.h>
using namespace std;
int n, m, ans;
vector<vector<int> > g, up;
int timer = 0;
vector<int> tin, tout, h;
void dfs(int v, int p, int cur_h) {
  tin[v] = timer++;
  h[v] = cur_h;
  if (v == 0) {
    up[v].assign(m, v);
  } else {
    up[v].resize(m);
    up[v][0] = p;
    for (int i = 1; i < m; ++i) {
      up[v][i] = up[up[v][i - 1]][i - 1];
    }
  }
  for (int i = 0; i < g[v].size(); ++i) {
    int to = g[v][i];
    if (to != p) {
      dfs(to, v, cur_h + 1);
    }
  }
  tout[v] = timer++;
}
bool belong(int a, int b) { return tin[a] <= tin[b] && tout[a] >= tout[b]; }
int lca(int a, int b) {
  if (belong(a, b)) {
    return a;
  } else if (belong(b, a)) {
    return b;
  }
  for (int i = m - 1; i >= 0; --i) {
    if (!belong(up[a][i], b)) {
      a = up[a][i];
    }
  }
  return up[a][0];
}
void solve(int s, int t, int f) {
  int u = lca(s, f), y = lca(t, f);
  int cur_ans = 0;
  cur_ans = min(abs(h[u] - h[f]), abs(h[y] - h[f]));
  if (u == y) {
    int w = lca(s, t);
    cur_ans += abs(h[u] - h[w]);
  }
  ans = max(ans, cur_ans);
}
int main(int argc, char *argv[]) {
  int q;
  scanf("%d%d", &n, &q);
  m = log2(n) + 5;
  g.resize(n);
  up.resize(n);
  tin.resize(n);
  tout.resize(n);
  h.resize(n);
  for (int i = 1; i < n; ++i) {
    int to;
    scanf("%d", &to);
    --to;
    g[i].push_back(to);
    g[to].push_back(i);
  }
  dfs(0, -1, 0);
  while (q--) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    --a;
    --b;
    --c;
    ans = 0;
    solve(a, b, c);
    solve(a, c, b);
    solve(b, a, c);
    solve(b, c, a);
    solve(c, a, b);
    solve(c, b, a);
    printf("%d\n", ans + 1);
  }
  return 0;
}
