#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
int head[maxn], cnt = 1, n, q;
struct Node {
  int v, nxt;
} G[maxn << 1];
int dist[maxn];
int f[maxn][25], deep[maxn];
void insert(int u, int v) {
  G[cnt] = (Node){v, head[u]};
  head[u] = cnt++;
}
void dfs(int x, int fa, int dep) {
  deep[x] = dep;
  f[x][0] = fa;
  for (int i = 1; i <= 20 && f[f[x][i - 1]][i - 1]; ++i)
    f[x][i] = f[f[x][i - 1]][i - 1];
  for (int i = head[x]; i; i = G[i].nxt) {
    int v = G[i].v;
    if (v == fa) continue;
    dist[v] = dist[x] + 1;
    dfs(v, x, dep + 1);
  }
}
int lca(int x, int y) {
  if (deep[x] < deep[y]) swap(x, y);
  for (int i = 20; i >= 0; --i)
    if (deep[f[x][i]] >= deep[y]) x = f[x][i];
  if (x == y) return x;
  for (int i = 20; i >= 0; --i)
    if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];
  return f[x][0];
}
int Dis(int x, int y) {
  int z = lca(x, y);
  return dist[x] + dist[y] - 2 * dist[z];
}
int work(int a, int b, int c) {
  return (Dis(a, b) + Dis(c, b) - Dis(a, c)) / 2;
}
int main() {
  scanf("%d%d", &n, &q);
  for (int i = 2; i <= n; ++i) {
    int p;
    scanf("%d", &p);
    insert(i, p);
    insert(p, i);
  }
  dfs(1, -1, 0);
  while (q--) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    printf("%d\n", max(work(a, b, c), max(work(a, c, b), work(b, a, c))) + 1);
  }
  return 0;
}
