#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 2;
int n, q, d[N];
int p[20][N], tin[N], tout[N], timer;
vector<int> g[N];
void dfs(int v, int pr) {
  p[0][v] = pr;
  for (int i = 1; i <= 17; i++) {
    p[i][v] = p[i - 1][p[i - 1][v]];
  }
  tin[v] = ++timer;
  for (int to : g[v])
    if (to != pr) {
      d[to] = d[v] + 1;
      dfs(to, v);
    }
  tout[v] = timer;
}
int 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 = 17; i >= 0; i--)
    if (!upper(p[i][a], b) && p[i][a]) a = p[i][a];
  return p[0][a];
}
int calc(int a, int b, int c) {
  int Lca = lca(a, b);
  if (!upper(Lca, c)) {
    return d[b] - d[Lca] + 1;
  } else {
    if (!upper(c, a) && !upper(c, b)) {
      for (int i = 17; i >= 0; i--) {
        if (p[i][c]) {
          if (!upper(p[i][c], a) && !upper(p[i][c], b)) {
            c = p[i][c];
          }
        }
      }
      c = p[0][c];
    }
    int lc = lca(c, b);
    return d[b] + d[c] - 2 * d[lc] + 1;
  }
}
int main() {
  cin >> n >> q;
  for (int i = 2; i <= n; i++) {
    int p;
    cin >> p;
    g[p].push_back(i);
    g[i].push_back(p);
  }
  dfs(1, 0);
  while (q--) {
    int a, b, c, ans = 0;
    cin >> a >> b >> c;
    ans = max(ans, calc(a, b, c));
    ans = max(ans, calc(a, c, b));
    ans = max(ans, calc(b, a, c));
    ans = max(ans, calc(b, c, a));
    ans = max(ans, calc(c, a, b));
    ans = max(ans, calc(c, b, a));
    cout << ans << endl;
  }
}
