#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, q;
int par[N], d[N], f[N][25];
vector<int> a[N];
void dfs(int u) {
  for (int i = 0; i < a[u].size(); ++i) {
    int v = a[u][i];
    if (par[v]) continue;
    par[v] = u;
    d[v] = d[u] + 1;
    dfs(v);
  }
}
void makelca() {
  for (int i = 1; i <= n; ++i) f[i][0] = par[i];
  for (int j = 1; j <= 20; ++j)
    for (int i = 1; i <= n; ++i)
      if (f[i][j - 1] != -1) f[i][j] = f[f[i][j - 1]][j - 1];
}
int lca(int u, int v) {
  if (d[u] < d[v]) swap(u, v);
  for (int i = 20; i >= 0; --i)
    if (d[u] - (1 << i) >= d[v]) u = f[u][i];
  if (u == v) return u;
  for (int i = 20; i >= 0; --i)
    if (f[u][i] != -1 && f[u][i] != f[v][i]) {
      u = f[u][i];
      v = f[v][i];
    }
  return f[u][0];
}
int dis(int u, int v) { return d[u] + d[v] - 2 * d[lca(u, v)]; }
int cal(int s, int t, int f) {
  int dis1 = dis(s, t) + 1;
  int dis2 = dis(t, f) + 1;
  int overlap = dis(s, f) + 1;
  return (dis1 + dis2 - overlap + 1) / 2;
}
int main() {
  scanf("%d%d", &n, &q);
  for (int i = 2, p; i <= n; ++i) {
    scanf("%d", &p);
    a[p].push_back(i);
    a[i].push_back(p);
  }
  par[1] = -1;
  dfs(1);
  makelca();
  for (int i = 1, x, y, z; i <= q; ++i) {
    scanf("%d%d%d", &x, &y, &z);
    int _1 = max(cal(x, y, z), cal(x, z, y));
    int _2 = max(cal(y, z, x), cal(y, x, z));
    int _3 = max(cal(z, x, y), cal(z, y, x));
    printf("%d\n", max(_1, max(_2, _3)));
  }
}
