#include <bits/stdc++.h>
using namespace std;
const long long M = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const int N = 2e5 + 10;
int n, q;
vector<int> g[N];
int a[5], dis[N];
int up[N][25];
void dfs(int u, int fa) {
  dis[u] = dis[fa] + 1;
  up[u][0] = fa;
  for (int i = 0; i < g[u].size(); i++) {
    int v = g[u][i];
    if (v == fa) continue;
    dfs(v, u);
  }
}
void initLCA() {
  for (int i = 1; i <= 20; i++) {
    for (int j = 1; j < n + 1; j++) up[j][i] = up[up[j][i - 1]][i - 1];
  }
}
int LCA(int u, int v) {
  int x = dis[u], y = dis[v];
  if (x < y) swap(u, v);
  int c = dis[u] - dis[v];
  for (int i = 0; i < 20; i++)
    if (c & (1 << i)) u = up[u][i];
  if (u == v) return v;
  for (int i = 20; i >= 0; i--) {
    if (up[u][i] != up[v][i]) {
      u = up[u][i];
      v = up[v][i];
    }
  }
  return up[u][0];
}
int calc(int x, int y) { return dis[x] + dis[y] - 2 * dis[LCA(x, y)] + 1; }
int solve(int s, int f, int t) {
  int x = LCA(s, t), y = LCA(f, t), z = LCA(f, s);
  if (x == s) {
    if (y == t) return calc(f, t);
    if (dis[y] > dis[s]) return calc(y, f);
    return calc(s, f);
  } else if (x == t) {
    if (z == s) return calc(s, f);
    if (dis[z] > dis[t]) return calc(z, f);
    return calc(t, f);
  } else {
    if (z == s) return calc(s, f);
    if (y == t) return calc(t, f);
    if (dis[z] == dis[y]) return calc(x, f);
    if (dis[z] > dis[y]) return calc(z, f);
    return calc(y, f);
  }
}
int main() {
  scanf("%d%d", &n, &q);
  int t;
  for (int i = 0; i < n - 1; i++) {
    scanf("%d", &t);
    g[t].push_back(i + 2);
    g[i + 2].push_back(t);
  }
  dfs(1, 0);
  initLCA();
  while (q--) {
    scanf("%d%d%d", &a[0], &a[1], &a[2]);
    sort(a, a + 3);
    int ans = 0;
    do {
      ans = max(ans, solve(a[0], a[1], a[2]));
    } while (next_permutation(a, a + 3));
    printf("%d\n", ans);
  }
  return 0;
}
