#include <bits/stdc++.h>
using namespace std;
const double eps = 0.000001;
const long double pi = acos(-1);
const int maxn = 1e7 + 9;
const int mod = 1e9 + 7;
const long long MOD = 1e18 + 9;
const long long INF = 1e18 + 123;
const int inf = 2e9 + 11;
const int minn = 1e5 + 9;
const int N = 5e3 + 123;
const int NN = 5e5 + 123;
const int PRI = 555557;
const int pri = 997;
int tests = 1, lim = 1;
int n, m, k, timer, ans;
vector<int> g[NN];
int lvl[NN], tin[NN], pr[NN][32], tout[NN];
void dfs(int v, int p = 1) {
  lvl[v] = lvl[p] + 1;
  tin[v] = ++timer;
  pr[v][0] = p;
  for (int i = 1; i <= lim; i++) pr[v][i] = pr[pr[v][i - 1]][i - 1];
  for (auto to : g[v]) {
    if (to != p) dfs(to, v);
  }
  tout[v] = ++timer;
}
bool upper(int a, int b) { return tin[a] <= tin[b] && tout[a] >= tout[b]; }
int lca(int a, int b) {
  if (upper(a, b)) return a;
  if (upper(b, a)) return b;
  for (int i = lim; i >= 0; i--)
    if (!upper(pr[a][i], b)) a = pr[a][i];
  return pr[a][0];
}
int dis(int a, int b) {
  int qwe = lca(a, b);
  return lvl[b] + lvl[a] - 2 * lvl[qwe];
}
int solve(int s, int t, int f) {
  int a = lca(s, f), b = lca(t, f);
  if (a == b) {
    int od = lca(s, t);
    return dis(od, f);
  }
  if (upper(a, b)) {
    return dis(b, f);
  } else {
    return dis(a, f);
  }
}
inline void Solve() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  cin >> n >> m;
  while (n >= (1 << lim)) ++lim;
  for (int i = 2; i <= n; i++) {
    int x;
    cin >> x;
    g[x].push_back(i);
    g[i].push_back(x);
  }
  dfs(1);
  for (int i = 1; i <= m; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    cout << max(solve(x, y, z), max(solve(y, z, x), solve(z, x, y))) + 1
         << '\n';
  }
}
int main() {
  while (tests--) {
    Solve();
  }
}
