#include <bits/stdc++.h>
using namespace std;
const int N = 100001;
const int inf = 2e9;
const int md = 1e9 + 7;
int n, q, c1[N], h[N], sz;
vector<int> a[N];
vector<pair<int, int> > c, d;
void dfs(int v, int pr = 0) {
  c1[v] = 1;
  for (int i = 0; i < a[v].size(); ++i) {
    int to = a[v][i];
    if (to == pr) continue;
    dfs(to, v);
    c1[v] += c1[to];
  }
}
int num[N], head[N], cp, cmp[N], ps, prd[N];
void dfs1(int v, int nm, int pr = 0) {
  cmp[v] = nm;
  h[v] = h[pr] + 1;
  prd[v] = pr;
  num[v] = ++ps;
  int mx = 0;
  int mxv = 0;
  for (int i = 0; i < a[v].size(); ++i) {
    int to = a[v][i];
    if (to == pr) continue;
    if (c1[to] > mx) {
      mx = c1[to];
      mxv = to;
    }
  }
  if (mxv != 0) {
    dfs1(mxv, nm, v);
  }
  for (int i = 0; i < a[v].size(); ++i) {
    int to = a[v][i];
    if (to == pr || to == mxv) continue;
    cp++;
    head[cp] = to;
    dfs1(to, cp, v);
  }
}
inline void upd(int a, int b, int x) {
  int l, r;
  while (cmp[a] != cmp[b]) {
    if (h[head[cmp[a]]] < h[head[cmp[b]]]) swap(a, b);
    l = num[head[cmp[a]]];
    r = num[a];
    if (x == 1)
      c.push_back(make_pair(l, r));
    else
      d.push_back(make_pair(l, r));
    a = prd[head[cmp[a]]];
  }
  if (h[a] > h[b]) swap(a, b);
  l = num[a];
  r = num[b];
  if (x == 1)
    c.push_back(make_pair(l, r));
  else
    d.push_back(make_pair(l, r));
}
int intersect(int a1, int a2, int a3, int a4) {
  return min(a2, a4) - max(a1, a3) + 1;
}
inline int get_ans() {
  int i = 0;
  int j = 0;
  int res = 0;
  while (i < c.size() && j < d.size()) {
    while (j < d.size() && d[j].second < c[i].first) j++;
    while (j < d.size() &&
           intersect(c[i].first, c[i].second, d[j].first, d[j].second) > 0) {
      res += intersect(c[i].first, c[i].second, d[j].first, d[j].second);
      if (d[j].second > c[i].second) {
        break;
      } else {
        j++;
      }
    }
    i++;
  }
  return res;
}
inline int get(int a, int b, int c) {
  upd(a, b, 1);
  upd(b, c, 2);
  sort(::c.begin(), ::c.end());
  sort(::d.begin(), ::d.end());
  int res = get_ans();
  ::c.clear();
  ::d.clear();
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n >> q;
  sz = 1;
  while (sz < n) sz *= 2;
  for (int i = 2; i <= n; ++i) {
    int x;
    cin >> x;
    a[x].push_back(i);
    a[i].push_back(x);
  }
  cp = 1;
  head[1] = 1;
  cmp[1] = 1;
  dfs(1, 1);
  dfs1(1, 1);
  for (int i = 1; i <= q; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    int mx = 0;
    mx = max(mx, get(a, b, c));
    mx = max(mx, get(a, c, b));
    mx = max(mx, get(b, a, c));
    mx = max(mx, get(b, c, a));
    mx = max(mx, get(c, a, b));
    mx = max(mx, get(c, b, a));
    cout << mx << '\n';
  }
}
