#include <bits/stdc++.h>
using namespace std;
long long int n, q, ct, ans;
vector<vector<long long int> > edge;
vector<long long int> depth, nodepos, rnodepos, A;
void swap(int &a, int &b) {
  int t = a;
  a = b;
  b = t;
}
void dfs(int src, int currdepth) {
  depth[src] = currdepth;
  nodepos[src] = ct;
  rnodepos[ct] = src;
  A[ct++] = currdepth;
  for (auto it : edge[src]) {
    dfs(it, currdepth + 1);
    rnodepos[ct] = src;
    A[ct++] = currdepth;
  }
}
class SparseTable {
  vector<vector<long long int> > table, idx;
  int maxk, n;

 public:
  int findsz(int n) {
    int ans = 0, curr = 1;
    while ((curr << 1) <= n) {
      curr = curr << 1;
      ans++;
    }
    return ans;
  }
  SparseTable() {}
  SparseTable(vector<long long int> &arr) {
    n = arr.size();
    maxk = findsz(n);
    table.resize(maxk + 1);
    idx.resize(maxk + 1);
    for (int i = 0; i <= maxk; i++) {
      table[i].resize(n);
      idx[i].resize(n);
    }
    for (int i = 0; i < n; i++) {
      table[0][i] = arr[i];
      idx[0][i] = i;
    }
    for (int k = 1; k <= maxk; k++) {
      for (int i = 0; i < n; i++) {
        int j = i + (1 << (k - 1));
        if (j < n) {
          if (table[k - 1][i] < table[k - 1][j]) {
            idx[k][i] = idx[k - 1][i];
            table[k][i] = table[k - 1][i];
          } else {
            idx[k][i] = idx[k - 1][j];
            table[k][i] = table[k - 1][j];
          }
        }
      }
    }
  }
  int minimum(int a, int b) {
    int len = b - a + 1;
    int k = findsz(len);
    int m = b - (1 << k) + 1;
    if (table[k][a] < table[k][m]) return idx[k][a];
    return idx[k][m];
  }
};
SparseTable *S;
int lca(int a, int b) {
  int x = nodepos[a];
  int y = nodepos[b];
  if (x > y) swap(x, y);
  int pos = S->minimum(x, y);
  return rnodepos[pos];
}
int dis(int u, int v) {
  long long int w = lca(u, v);
  return depth[u] + depth[v] - 2 * depth[w];
}
void solve(int s, int t, int f) {}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cin >> n >> q;
  ct = 0;
  edge.resize(n);
  depth.resize(n);
  nodepos.resize(n);
  A.resize(2 * n - 1);
  rnodepos.resize(2 * n - 1);
  for (int i = 2; i <= n; i++) {
    int tmp;
    cin >> tmp;
    edge[tmp - 1].push_back(i - 1);
  }
  dfs(0, 0);
  S = new SparseTable(A);
  for (int i = 0; i < q; i++) {
    long long int a, b, c;
    cin >> a >> b >> c;
    --a;
    --b;
    --c;
    long long int l1 = lca(a, b), l2 = lca(a, c), l3 = lca(b, c);
    long long int l;
    if (l1 == l2)
      l = l3;
    else if (l2 == l3)
      l = l1;
    else
      l = l2;
    cout << 1 + max(dis(l, a), max(dis(l, b), dis(l, c))) << endl;
  }
  return 0;
}
