#include <bits/stdc++.h>   using namespace std;   const int MX = 1e5 + 69;   const int inf = 1e9 + 5;   const long long mod = 1e9 + 7;   const long double eps = 1e-10;   vector<int> v[MX];   int par[24][MX];   int depth[MX];   void dfs(int x, int p) {     for (auto g : v[x]) {       if (g == p) {         continue;       }       depth[g] = depth[x] + 1;       dfs(g, x);       par[0][g] = x;     }   }   int lca(int x, int y) {     if (depth[x] > depth[y]) swap(x, y);     int i = 22;     while (depth[x] < depth[y]) {       if (depth[par[i][y]] >= depth[x]) {         y = par[i][y];       }       i--;     }     for (int i = 22; i >= 0; i--) {       if (par[i][x] != par[i][y]) {         x = par[i][x];         y = par[i][y];       }     }     if (x != y) {       x = par[0][x];       y = par[0][y];     }     return x;   }   int dis(int a, int b) { return (depth[a] + depth[b] - 2 * depth[lca(a, b)]); }   int cal(int a, int b, int c) {     int ret = 0;     int q1 = lca(a, b);     int q2 = lca(a, c);     int q3 = lca(b, c);     if (depth[q1] >= max(depth[q2], depth[q3])) {       ret = dis(lca(a, b), c);     } else {       ret = dis((depth[q2] >= depth[q3] ? q2 : q3), c);     }     return ret;   }   int main() {     depth[0] = -1;     int n, q;     cin >> n >> q;     for (int i = 2; i <= n; i++) {       int b;       scanf( %d , &b);       v[i].push_back(b);       v[b].push_back(i);     }     dfs(1, -1);     for (int i = 1; i < 23; i++) {       for (int j = 1; j <= n; j++) {         par[i][j] = par[i - 1][par[i - 1][j]];       }     }     while (q--) {       int ans = 0;       int a, b, c;       scanf( %d%d%d , &a, &b, &c);       ans = max(cal(a, b, c), max(cal(a, c, b), cal(b, c, a)));       printf( %d n , ans + 1);     }     return 0;   }   