#include <bits/stdc++.h>
using namespace std;
const long long int INF = 1e18;
const int inf = 1e9;
const int MOD = 1e9 + 7;
const int nax = 1000000 + 10;
vector<int> graph[nax];
vector<int> h(nax);
int par[nax][32];
const int MAXLOG = 20;
void dfs(int v, int p = -1) {
  par[v][0] = p;
  if (p != -1) h[v] = h[p] + 1;
  for (int i = 1; i < MAXLOG; i++) {
    if (par[v][i - 1] != -1) par[v][i] = par[par[v][i - 1]][i - 1];
  }
  for (auto x : graph[v]) {
    if (x != p) dfs(x, v);
  }
}
int query(int u, int v) {
  if (h[v] < h[u]) swap(v, u);
  for (int i = MAXLOG - 1; i >= 0; i--) {
    if (par[v][i] != -1 && h[par[v][i]] >= h[u]) v = par[v][i];
  }
  if (u == v) return u;
  for (int i = MAXLOG - 1; i >= 0; i--) {
    if (par[u][i] != par[v][i]) {
      u = par[u][i];
      v = par[v][i];
    }
  }
  return par[v][0];
}
void clr() { memset(par, -1, sizeof(par)); }
int dist(int u, int v) { return h[u] + h[v] - 2 * h[query(u, v)]; }
int solve(int a, int b, int c) {
  int l1 = query(a, b), l2 = query(b, c), l3 = query(a, c);
  int l;
  if (l1 == l2)
    l = l3;
  else if (l2 == l3)
    l = l1;
  else
    l = l2;
  int ans = dist(a, l);
  return ans;
}
int n;
int main() {
  clr();
  cin >> n;
  ;
  int q;
  cin >> q;
  for (int i = 2; i <= n; i++) {
    int v;
    cin >> v;
    graph[i].push_back(v);
    graph[v].push_back(i);
  }
  dfs(1);
  while (q--) {
    int a, b, c;
    cin >> a >> b >> c;
    int ans = solve(a, b, c);
    ans = max(ans, solve(a, c, b));
    ans = max(ans, solve(b, c, a));
    ans = max(ans, solve(b, a, c));
    ans = max(ans, solve(c, a, b));
    ans = max(ans, solve(c, b, a));
    cout << ans + 1 << endl;
  }
  return 0;
}
