#include <bits/stdc++.h>
using namespace std;
const long long maxn = 2e5;
const long long mod = 1e9 + 7;
const long double PI = 4 * atan((long double)1);
long long pw(long long a, long long b, long long md = mod) {
  return (!b ? 1
             : (b & 1 ? a * pw(a * a % md, b / 2, md) % md
                      : pw(a * a % md, b / 2, md) % md));
}
int n, q;
int par[maxn][50];
vector<int> adj[maxn];
int h[maxn];
void dfs(int v = 1) {
  for (auto u : adj[v]) {
    h[u] = h[v] + 1;
    dfs(u);
  }
}
int lca(int u, int v) {
  if (h[u] > h[v]) {
    swap(u, v);
  }
  for (int i = 35; i >= 0; i--) {
    if (par[v][i] and h[par[v][i]] >= h[u]) {
      v = par[v][i];
    }
  }
  if (v == u) {
    return (v);
  }
  for (int i = 35; i >= 0; i--) {
    if (par[v][i] != par[u][i]) {
      v = par[v][i], u = par[u][i];
    }
  }
  if (par[u][0] != par[v][0]) {
    return (-1);
  }
  return (par[v][0]);
}
int dist(int u, int v) {
  int lc = lca(u, v);
  return (abs(h[u] - h[lc]) + abs(h[v] - h[lc]));
}
int solve(int a, int b, int c) {
  return (abs(dist(a, c) + dist(b, c) - dist(a, b)) / 2 + 1);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> q;
  for (int i = 2; i <= n; i++) {
    cin >> par[i][0];
    adj[par[i][0]].push_back(i);
    ;
  }
  for (int j = 1; j < 40; j++) {
    for (int i = 1; i <= n; i++) {
      par[i][j] = par[par[i][j - 1]][j - 1];
    }
  }
  dfs();
  while (q--) {
    int a[3];
    int mx = 0;
    for (int i = 0; i < 3; i++) {
      cin >> a[i];
    }
    int b[] = {0, 1, 2};
    for (int i = 0; i < 6; i++) {
      mx = max(mx, solve(a[b[0]], a[b[1]], a[b[2]]));
      next_permutation(b, b + 3);
    }
    cout << mx << '\n';
  }
  return (0);
}
