#include<bits/stdc++.h>
#include<vector>
#include <utility>
#define int long long int
//#define int long long
using namespace std;
struct node
{
int val;
int dis;
int pa;
node* parent=nullptr;
vector<node*> children;
};
int maxroot=-(int)1e9;
int maxdis=-(int)1e9;
node* nodelib[10001];
node* creatnewnode(int tempval,int tempdis)
{
node *newnode= new node;
newnode->val=tempval;
newnode->dis=tempdis;
return newnode;
}
int which(int a,int b)
{
if(a>b)
return a;
return b;
}
void start()
{
int tempval,tempdis,temparent;
cin>>temparent>>tempval>>tempdis;
if(nodelib[tempval]!=nullptr)
{
nodelib[tempval]->val=tempval;
nodelib[tempval]->dis=tempdis;
nodelib[tempval]->pa=temparent;
}
else
{
node* newnode=creatnewnode(tempval,tempdis);
newnode->pa=temparent;
nodelib[tempval]=newnode;
}
if(nodelib[temparent]!=nullptr)
{
nodelib[tempval]->parent=nodelib[temparent];
nodelib[temparent]->children.emplace_back(nodelib[tempval]);
}
else
{
node *newroot=new node;
nodelib[temparent]=newroot;
nodelib[tempval]->parent=newroot;
newroot->children.emplace_back(nodelib[tempval]);
}
return;
}
int bigger(int l,int f)
{
return l>f;
}
int check(node* root)
{
if(root==nullptr)
return 0;
if(root->children.size()==0)
{
if(root->dis > maxdis)
{
maxdis=root->dis;
maxroot=root->val;
}
return root->dis;
}
vector<int> subtree;
for(int i=0;i<root->children.size();i++)
subtree.emplace_back(check(root->children[i]));
sort(subtree.begin(),subtree.end(),bigger);
if(subtree.size()>1 && subtree[0]+subtree[1]+root->dis > maxdis)
{
maxdis=subtree[0]+subtree[1]+root->dis;
maxroot=root->val;
}
if(subtree.size()==1 && subtree[0]+root->dis > maxdis)
{
maxdis=subtree[0]+root->dis;
maxroot=root->val;
}
int oneside;
if(subtree.size()==1)
oneside=subtree[0];
else
oneside=which(subtree[0],subtree[1]);
if(oneside<0)
{
if(root->dis>maxdis)
{
maxdis=root->dis;
maxroot=root->val;
}
return root->dis;
}
else
{
if(root->dis+oneside>maxdis)
{
maxdis=root->dis+oneside;
maxroot=root->val;
}
return root->dis+oneside;
}
}
void initial()
{
for(int i=0;i<10001;i++)
{
//cout<<nodelib[i]<<endl;
nodelib[i]=nullptr;
//cout<<nodelib[i]<<endl;
}
}
signed main()
{
cin.tie(NULL),cout.tie(NULL);
ios::sync_with_stdio(0);
//initial();
int n,o;
cin>>n>>o;
int tempval,tempdis,temparent;
cin>>tempval>>tempdis;
node *root=creatnewnode(tempval,tempdis);
nodelib[tempval]=root;
for(int i=0;i<n;i++)
{
start();
}
while(o--)
{
string command;
cin>>command;
if(command=="Add")
{
start();
}
else if(command=="Delete")
{
int target;
cin>>target;
if(nodelib[target]==NULL)
goto end;
node *tempa=nodelib[target]->parent;
node *tempdelete=nodelib[target];
for(int i=0;i<tempa->children.size();i++)
{
if(tempdelete==tempa->children[i])
{
tempa->children.erase(tempa->children.begin()+i);
break;
}
}
if(tempdelete->children.size()!=0)
{
for(int i=0;i<tempdelete->children.size();i++)
{
tempdelete->children[i]->parent=tempa;
tempdelete->children[i]->pa=tempa->val;
tempa->children.emplace_back(tempdelete->children[i]);
}
}
/*for(auto i=tempa->children.begin();i!=tempa->children.end();i++)
cout<<(*i)->val<<", ";
cout<<endl;
*/
nodelib[target]=nullptr;
delete tempdelete;
}
else if(command=="Check")
{
check(root);
cout<<"Maximum Value: "<<maxdis<<endl;
cout<<"Root of the Path: "<<maxroot<<endl;
maxdis=-(int)1e9;
}
end:;
}
check(root);
cout<<"Final Root: "<<maxroot<<endl;
}