#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef long long ll;
typedef pair<long long, long long> l4;
typedef vector<long long> vll;
typedef double db;
typedef vector<double> vdb;
typedef pair<double, double> dd;
typedef set<int> si;
typedef set<long long> sll;
#define fi first
#define se second
#define matrix(a) vector< vector<a> >
#define sz(a) int((a).size())
#define lop(i,a,b) for (int i=a; i<=b; i++)
#define vlop(i,v) lop(i,0,sz(v)-1)
#define vlop1(i,v) lop(i,1,sz(v)-1)
#define rlop(i,a,b) for (int i=b; i>=a; i--)
#define vrlop(i,v) rlop(i,0,sz(v)-1)
#define vrlop1(i,v) rlop(i,1,sz(v)-1)
#define printv(i,v) vlop(i,v)cout<<v[i]<<" "
#define printv1(i,v) vlop1(i,v)cout<<v[i]<<" "
#define all(s) (s).begin(),(s).end()
#define pb push_back
#define enter cout<<'\n'
#define lb(i,v) int(lower_bound(all(v),i)-v.begin())
#define ub(i,v) int(upper_bound(all(v),i)-v.begin())
void initialize(int start, vi& stree, vi& level, int l, int u) {
if (sz(stree) - 1 < start)stree.resize(start + 1);
if (l == u)stree[start] = l;
else {
initialize(2 * start, stree, level, l, (u + l) / 2);
initialize(2 * start + 1, stree, level, (u + l) / 2 + 1, u);
if (level[stree[start * 2]] < level[stree[start * 2 + 1]])stree[start] = stree[start * 2];
else stree[start] = stree[start * 2 + 1];
}
return;
}
int rmq(int start, vi& stree, vi& level, int i, int j, int l, int u) {
if (j<l || i>u)return -1;
if (l >= i&&u <= j)return stree[start];
int t1 = rmq(2 * start, stree, level, i, j, l, (u + l) / 2);
int t2 = rmq(2 * start + 1, stree, level, i, j, (u + l) / 2 + 1, u);
if (t1 == -1)return t2;
if (t2 == -1)return t1;
if (level[t1] < level[t2])return t1;
return t2;
}
void dfs(matrix(int)& alist, vi& tour, int root, vi& entry, vi& level, vi& visited,int currentlevel) {
visited[root] = 1;
tour.pb(root);
level.pb(currentlevel);
entry[root] = sz(tour) - 1;
vlop(i, alist[root]) {
if (!visited[alist[root][i]]) {
dfs(alist, tour, alist[root][i], entry,level, visited,currentlevel+1);
tour.pb(root);
level.pb(currentlevel);
}
}
return;
}
int main(){
ios::sync_with_stdio(false);
int n,q;
cin>>n>>q;
matrix(int) alist(n+1);
int p;
lop(i,2,n){
cin>>p;
alist[i].pb(p);
alist[p].pb(i);
}
vi tour,level,entry(n + 1),visited(n + 1, 0),stree;
dfs(alist, tour, 1, entry, level, visited,0);
initialize(1, stree, level, 0, sz(level)-1);
int a,b,c,ab,ac,bc,lab,lac,lbc,la,lb,lc;
lop(query,1,q){
cin>>a>>b>>c;
ab=tour[rmq(1, stree, level, min(entry[a],entry[b]), max(entry[a],entry[b]), 0, sz(level) - 1)];
ac=tour[rmq(1, stree, level, min(entry[a],entry[c]), max(entry[a],entry[c]), 0, sz(level) - 1)];
bc=tour[rmq(1, stree, level, min(entry[c],entry[b]), max(entry[c],entry[b]), 0, sz(level) - 1)];
lab=level[entry[a]]+level[entry[b]]-level[entry[ab]]*2;
lac=level[entry[a]]+level[entry[c]]-level[entry[ac]]*2;
lbc=level[entry[b]]+level[entry[c]]-level[entry[bc]]*2;
la=(lab+lac-lbc)/2;
lb=(lab+lbc-lac)/2;
lc=(lbc+lac-lab)/2;
cout<<max(la,max(lb,lc))+1<<'\n';
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwp0eXBlZGVmIHZlY3RvcjxpbnQ+IHZpOwp0eXBlZGVmIHBhaXI8aW50LCBpbnQ+IGlpOwp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKdHlwZWRlZiBwYWlyPGxvbmcgbG9uZywgbG9uZyBsb25nPiBsNDsKdHlwZWRlZiB2ZWN0b3I8bG9uZyBsb25nPiB2bGw7CnR5cGVkZWYgZG91YmxlIGRiOwp0eXBlZGVmIHZlY3Rvcjxkb3VibGU+IHZkYjsKdHlwZWRlZiBwYWlyPGRvdWJsZSwgZG91YmxlPiBk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