#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
vector<int> g[101000];
int T[101000];
int P[101000][20];
int L[101000];
void dfs(int u = 1, int par = 1, int l = 1) {
  T[u] = par;
  L[u] = l;
  for (int v : g[u])
    if (T[v] == 0) dfs(v, u, l + 1);
}
int n, q;
int lca(int p, int q) {
  int tmp, log, i;
  if (L[p] < L[q]) tmp = p, p = q, q = tmp;
  for (log = 1; 1 << log <= L[p]; log++)
    ;
  log--;
  for (i = log; i >= 0; i--)
    if (L[p] - (1 << i) >= L[q]) p = P[p][i];
  if (p == q) return p;
  for (i = log; i >= 0; i--)
    if (P[p][i] != -1 && P[p][i] != P[q][i]) p = P[p][i], q = P[q][i];
  return T[p];
}
int dist(int a, int b) {
  int ab = lca(a, b);
  return L[a] + L[b] - 2 * L[ab] + 1;
}
int calc(int s, int f, int t) {
  return (dist(s, f) + dist(f, t) - dist(s, t) + 1) / 2;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> q;
  for (int i = 2; i <= n; ++i) {
    int j;
    cin >> j;
    g[i].push_back(j);
    g[j].push_back(i);
  }
  dfs();
  int i, j;
  for (i = 1; i <= n; i++)
    for (j = 0; 1 << j < n; j++) P[i][j] = -1;
  for (i = 1; i <= n; i++) P[i][0] = T[i];
  for (j = 1; 1 << j < n; j++)
    for (i = 1; i <= n; i++)
      if (P[i][j - 1] != -1) P[i][j] = P[P[i][j - 1]][j - 1];
  for (i = 0; i < q; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    cout << max(calc(a, b, c), max(calc(b, a, c), calc(a, c, b))) << '\n';
  }
}
