#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
const int inf = 0x3f3f3f3f;
int deep[maxn], fa[maxn][32];
vector<int> edge[maxn];
int flag[maxn];
void dfs(int u) {
  flag[u] = 1;
  for (auto i : edge[u]) {
    int t = i;
    if (flag[t]) continue;
    deep[t] = deep[u] + 1;
    fa[t][0] = u;
    dfs(t);
  }
}
int lca(int x, int y) {
  if (deep[x] > deep[y]) swap(x, y);
  for (int i = 30; i >= 0; i--)
    if (deep[fa[y][i]] >= deep[x]) y = fa[y][i];
  if (x == y) return x;
  for (int i = 30; i >= 0; i--)
    if (fa[x][i] != fa[y][i]) {
      x = fa[x][i], y = fa[y][i];
    }
  return fa[x][0];
}
void init(int n) {
  deep[1] = 1;
  fa[1][0] = 1;
  dfs(1);
  for (int i = (1); i < (30); i++)
    for (int j = (1); j < (n + 1); j++) fa[j][i] = fa[fa[j][i - 1]][i - 1];
}
int dis(int a, int b) { return deep[a] + deep[b] - 2 * deep[lca(a, b)]; }
int main() {
  int n, q;
  cin >> n >> q;
  for (int i = (1); i < (n); i++) {
    int p;
    cin >> p;
    edge[p].push_back(i + 1);
    edge[i + 1].push_back(p);
  }
  init(n);
  while (q--) {
    int aans = 0;
    int ii[3];
    cin >> ii[0] >> ii[1] >> ii[2];
    for (int i = (0); i < (3); i++)
      aans = max(dis(ii[i], ii[(i + 1) % 3]) + dis(ii[i], ii[(i + 2) % 3]) -
                         dis(ii[(i + 1) % 3], ii[(i + 2) % 3]) >>
                     1,
                 aans);
    cout << aans + 1 << endl;
  }
  return 0;
}
