#include <bits/stdc++.h>
using namespace std;
const long long infl = 1e18 + 5;
long long int n, m, k, q, x, y, f, val, t = 1, i, j;
long long int ind = -1, cnt, sz, sm, ans, mx = -1, mn = infl;
long long int d[100004], par[17][100004];
std::vector<int> g[100004];
void dfs(int u, int p) {
  par[0][u] = p;
  int i;
  for (i = 1; i < 17; i++) {
    par[i][u] = par[i - 1][par[i - 1][u]];
  }
  for (auto it : g[u]) {
    if (it == p) continue;
    d[it] = d[u] + 1;
    dfs(it, u);
  }
}
int get_parent(int u, int k) {
  int i;
  for (i = 0; i < 17; i++) {
    if ((1 << i) & k) {
      u = par[i][u];
    }
  }
  return u;
}
int lca(int x, int y) {
  if (d[x] < d[y]) swap(x, y);
  x = get_parent(x, d[x] - d[y]);
  if (x == y) return x;
  int i;
  for (i = 17 - 1; i >= 0; i--) {
    if (par[i][x] != par[i][y]) {
      x = par[i][x];
      y = par[i][y];
    }
  }
  return par[0][x];
}
int dist(int x, int y) {
  int p = lca(x, y);
  int k = -(2 * d[p] - (d[x] + d[y]));
  return k + 1;
}
int cal(int x, int y, int z) {
  return (dist(x, y) + dist(z, y) - dist(x, z) + 1) / 2;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  if (fopen("inp.txt", "r")) {
    freopen("myfile.txt", "w", stdout);
    freopen("inp.txt", "r", stdin);
  }
  cin >> n >> q;
  for (i = 0; i < n - 1; i++) {
    cin >> x;
    x--;
    g[i + 1].push_back(x);
    g[x].push_back(i + 1);
  }
  dfs(0, 0);
  long long int z;
  while (q--) {
    cin >> x >> y >> z;
    x--;
    y--;
    z--;
    int ans = 0;
    ans = max(ans, cal(x, y, z));
    ans = max(ans, cal(y, z, x));
    ans = max(ans, cal(z, x, y));
    cout << ans << "\n";
  }
  return 0;
}
