#include <iostream>
#include <vector>
using namespace std;
int depth = 0;
void dfs(int node, vector<vector<int>> &graph, vector<int> & depthOf, int & leaf){
for(int child : graph[node])
if(!depthOf[child]){
depthOf[child] = ++depth;
if(depthOf[leaf] < depthOf[child])
leaf = child;
dfs(child, graph, depthOf, leaf);
depth--;
}
}
int main(){
int nodes, edges, firstLeaf = 0, secondLeaf = 0;
cin >> nodes >> edges;
vector<vector<int>> graph(nodes + 1);
vector<int> depthOf(nodes + 1), beZero(nodes + 1),
path(nodes + 1);
while(edges--){
int from, to;
cin >> from >> to;
graph[from].push_back(to);
graph[to].push_back(from);
}
// to get firstLeaf
int node = 1; // root
if(!depthOf[node]){
depthOf[node] = ++depth;
if(depthOf[firstLeaf] < depthOf[node])
firstLeaf = node;
dfs(node, graph, depthOf, firstLeaf);
}
// to get SecondLeaf
node = firstLeaf; // firstLeaf
depthOf = beZero;
depth = 0;
if(!depthOf[node]){
depthOf[node] = ++depth;
if(depthOf[secondLeaf] < depthOf[node])
secondLeaf = node;
dfs(node, graph, depthOf, secondLeaf);
}
cout << endl;
cout <<"First Leaf = "<< firstLeaf << endl;
cout <<"Second Leaf = "<< secondLeaf << endl;
cout <<"Longest Path (edges) = "<< depthOf[secondLeaf] - 1 << endl;
cout <<"Longest Path (nodes) = "<< depthOf[secondLeaf] << endl;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IGRlcHRoID0gMDsKdm9pZCBkZnMoaW50IG5vZGUsIHZlY3Rvcjx2ZWN0b3I8aW50Pj4gJmdyYXBoLCB2ZWN0b3I8aW50PiAmIGRlcHRoT2YsIGludCAmIGxlYWYpewogICAgZm9yKGludCBjaGlsZCA6IGdyYXBoW25vZGVdKQogICAgICAgIGlmKCFkZXB0aE9mW2NoaWxkXSl7CgogICAgICAgICAgICBkZXB0aE9mW2NoaWxkXSA9ICsrZGVwdGg7CgogICAgICAgICAgICBpZih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