#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100 * 1000 + 10, L = 20;
int n;
vector<int> adj[MAXN];
int par[L + 2][MAXN], hei[MAXN];
void lca_dfs(int u, int p) {
  hei[u] = hei[p] + 1;
  par[0][u] = p;
  for (int i = 1; i < L; i++) par[i][u] = par[i - 1][par[i - 1][u]];
  for (int v : adj[u])
    if (v != p) lca_dfs(v, u);
}
int find_par(int u, int x) {
  for (int i = L - 1; i >= 0; i--)
    if (x & (1 << i)) u = par[i][u];
  return u;
}
int lca(int u, int v) {
  if (hei[u] < hei[v]) swap(u, v);
  u = find_par(u, hei[u] - hei[v]);
  if (u == v) return u;
  for (int i = L - 1; i >= 0; i--)
    if (par[i][u] != par[i][v]) u = par[i][u], v = par[i][v];
  return par[0][u];
}
int count(int f, int s, int t) {
  int l1 = lca(f, s), l2 = lca(f, t);
  if (l1 != l2) return (hei[f] - max(hei[l1], hei[l2]) + 1);
  int l3 = lca(s, t);
  return (hei[f] - hei[l1] + hei[l3] - hei[l1] + 1);
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int q;
  cin >> n >> q;
  for (int i = 1; i < n; i++) {
    int u;
    cin >> u;
    u--;
    adj[u].push_back(i);
    adj[i].push_back(u);
  }
  lca_dfs(0, 0);
  while (q--) {
    int a[3];
    for (int i = 0; i < 3; i++) {
      cin >> a[i];
      a[i]--;
    }
    int ans = 0;
    for (int i = 0; i < 3; i++)
      (ans) = max((ans), (count(a[i], a[(i + 1) % 3], a[(i + 2) % 3])));
    cout << ans << "\n";
  }
  return 0;
}
