#include <bits/stdc++.h>
using namespace std;
const int N = 100050;
const int B = 20;
int dep[N], fa[N][B];
int n, q, a, b, c;
int lca(int x, int y) {
  if (dep[x] < dep[y]) swap(x, y);
  int p = dep[x] - dep[y];
  for (int i = 0; i < B; ++i)
    if ((p >> i) & 1) x = fa[x][i];
  if (x == y) return x;
  for (int i = B - 1; ~i; --i)
    if (fa[x][i] != fa[y][i]) {
      x = fa[x][i];
      y = fa[y][i];
    }
  return fa[x][0];
}
inline int calc(int s, int f, int t) {
  int l1 = lca(s, f), l2 = lca(t, f);
  int ans = dep[f] - max(dep[l1], dep[l2]) + 1;
  if (l1 == l2) ans += dep[lca(s, t)] - dep[l1];
  return ans;
}
void get(int x) {
  if (dep[x]) return;
  get(fa[x][0]);
  dep[x] = dep[fa[x][0]] + 1;
  for (int i = 1; (1 << i) < dep[x]; ++i) fa[x][i] = fa[fa[x][i - 1]][i - 1];
}
int main() {
  scanf("%d%d", &n, &q);
  dep[1] = 1;
  for (int x = 2; x <= n; ++x) scanf("%d", &fa[x][0]);
  for (int x = 2; x <= n; ++x) get(x);
  while (q--) {
    scanf("%d%d%d", &a, &b, &c);
    int ans = max(calc(a, b, c), calc(b, c, a));
    printf("%d\n", max(ans, calc(c, a, b)));
  }
  return 0;
}
