#!/usr/bin/env python

# -*- coding: utf-8 -*-



"""

input:

4

0 1 2

1 2 1

1 3 3



output:

5

"""



import sys

from collections import deque

from math import isinf





def generate_adj_table(v_table):

    for each in v_table:

        source, target, cost = list(map(int, each))

        init_adj_table[source].setdefault(target, cost)

        init_adj_table[target].setdefault(source, cost)



    return init_adj_table





def graph_bfs(v_init):

    global distance

    distance = [float('inf')] * vertices

    distance[v_init] = 0

    queue.appendleft(v_init)



    while queue:

        current = queue.popleft()

        adj_weight = adj_table[current]

        for adj in list(adj_weight.keys()):

            if isinf(distance[adj]):

                distance[adj] = distance[current] + adj_weight[adj]

                queue.append(adj)

    return None





def solve():

    graph_bfs(init_v)

    diameter, bridge_v = 0, 0

    for v in range(vertices):

        if isinf(distance[v]):

            continue

        if diameter < distance[v]:

            diameter = distance[v]

            bridge_v = v



    graph_bfs(bridge_v)

    diameter = 0

    for v in range(vertices):

        if isinf(distance[v]):

            continue

        diameter = max(diameter, distance[v])



    return diameter





if __name__ == '__main__':

    _input = sys.stdin.readlines()

    vertices = int(_input[0])

    v_info = [x.split() for x in _input[1:]]



    queue = deque()

    distance = [float('inf')] * vertices



    init_adj_table = tuple(dict() for _ in range(vertices))

    adj_table = generate_adj_table(v_info)



    init_v = 0

    print((solve()))
#!/usr/bin/env python

# -*- coding: utf-8 -*-



"""

input:

4

0 1 2

1 2 1

1 3 3



output:

5

"""



import sys

from math import isinf

from collections import deque





def generate_adj_table(v_table, init_adj_table):

    for each in v_table:

        source, target, cost = list(map(int, each))

        init_adj_table[source][target] = cost

        init_adj_table[target][source] = cost



    return init_adj_table





def graph_bfs(current, v_num, adj_table):

    queue = deque()

    distance = [float('inf')] * v_num

    distance[current] = 0

    queue.appendleft(current)



    while queue:

        current = queue.popleft()

        for adj, cost in list(adj_table[current].items()):

            if isinf(distance[adj]):

                distance[adj] = distance[current] + cost

                queue.append(adj)

    return distance





def calc_tree_diameter(v_num, adj_table):

    # init x -> bridge vertex

    init_v = 0

    distance_1 = graph_bfs(init_v, v_num, adj_table)

    diameter, bridge_v = 0, 0

    for v, dis_1 in enumerate(distance_1):

        if isinf(dis_1):

            continue

        elif diameter < dis_1:

            diameter = dis_1

            bridge_v = v



    # bridge vertex -> y

    distance_2 = graph_bfs(bridge_v, v_num, adj_table)

    diameter = 0

    for dis_2 in distance_2:

        if isinf(dis_2):

            continue

        diameter = max(diameter, dis_2)



    return diameter





def solve():

    _input = sys.stdin.readlines()

    v_num = int(_input[0])

    edges = [x.split() for x in _input[1:]]



    init_adj_table = tuple(dict() for _ in range(v_num))

    adj_table = generate_adj_table(edges, init_adj_table)

    ans = calc_tree_diameter(v_num, adj_table)

    print(ans)

    return None





if __name__ == '__main__':

    solve()
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
input:
4
0 1 2
1 2 1
1 3 3
output:
5
"""
import sys
from collections import deque
from math import isinf


def generate_adj_table(v_table):
    for each in v_table:
        source, target, cost = list(map(int, each))
        init_adj_table[source].setdefault(target, cost)
        init_adj_table[target].setdefault(source, cost)
    return init_adj_table


def graph_bfs(v_init):
    global distance
    distance = [float("inf")] * vertices
    distance[v_init] = 0
    queue.appendleft(v_init)
    while queue:
        current = queue.popleft()
        adj_weight = adj_table[current]
        for adj in list(adj_weight.keys()):
            if isinf(distance[adj]):
                distance[adj] = distance[current] + adj_weight[adj]
                queue.append(adj)
    return None


def solve():
    graph_bfs(init_v)
    diameter, bridge_v = 0, 0
    for v in range(vertices):
        if isinf(distance[v]):
            continue
        if diameter < distance[v]:
            diameter = distance[v]
            bridge_v = v
    graph_bfs(bridge_v)
    diameter = 0
    for v in range(vertices):
        if isinf(distance[v]):
            continue
        diameter = max(diameter, distance[v])
    return diameter


if __name__ == "__main__":
    _input = sys.stdin.readlines()
    vertices = int(_input[0])
    v_info = [x.split() for x in _input[1:]]
    queue = deque()
    distance = [float("inf")] * vertices
    init_adj_table = tuple(dict() for _ in range(vertices))
    adj_table = generate_adj_table(v_info)
    init_v = 0
    print((solve()))

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
input:
4
0 1 2
1 2 1
1 3 3
output:
5
"""
import sys
from math import isinf
from collections import deque


def generate_adj_table(v_table, init_adj_table):
    for each in v_table:
        source, target, cost = list(map(int, each))
        init_adj_table[source][target] = cost
        init_adj_table[target][source] = cost
    return init_adj_table


def graph_bfs(current, v_num, adj_table):
    queue = deque()
    distance = [float("inf")] * v_num
    distance[current] = 0
    queue.appendleft(current)
    while queue:
        current = queue.popleft()
        for adj, cost in list(adj_table[current].items()):
            if isinf(distance[adj]):
                distance[adj] = distance[current] + cost
                queue.append(adj)
    return distance


def calc_tree_diameter(v_num, adj_table):
    # init x -> bridge vertex
    init_v = 0
    distance_1 = graph_bfs(init_v, v_num, adj_table)
    diameter, bridge_v = 0, 0
    for v, dis_1 in enumerate(distance_1):
        if isinf(dis_1):
            continue
        elif diameter < dis_1:
            diameter = dis_1
            bridge_v = v
    # bridge vertex -> y
    distance_2 = graph_bfs(bridge_v, v_num, adj_table)
    diameter = 0
    for dis_2 in distance_2:
        if isinf(dis_2):
            continue
        diameter = max(diameter, dis_2)
    return diameter


def solve():
    _input = sys.stdin.readlines()
    v_num = int(_input[0])
    edges = [x.split() for x in _input[1:]]
    init_adj_table = tuple(dict() for _ in range(v_num))
    adj_table = generate_adj_table(edges, init_adj_table)
    ans = calc_tree_diameter(v_num, adj_table)
    print(ans)
    return None


if __name__ == "__main__":
    solve()

+from math import isinf -from math import isinf -def generate_adj_table(v_table): +def generate_adj_table(v_table, init_adj_table): -        init_adj_table[source].setdefault(target, cost) -        init_adj_table[target].setdefault(source, cost) +        init_adj_table[source][target] = cost +        init_adj_table[target][source] = cost -def graph_bfs(v_init): -    global distance -    distance = [float("inf")] * vertices -    distance[v_init] = 0 -    queue.appendleft(v_init) +def graph_bfs(current, v_num, adj_table): +    queue = deque() +    distance = [float("inf")] * v_num +    distance[current] = 0 +    queue.appendleft(current) -        adj_weight = adj_table[current] -        for adj in list(adj_weight.keys()): +        for adj, cost in list(adj_table[current].items()): -                distance[adj] = distance[current] + adj_weight[adj] +                distance[adj] = distance[current] + cost +    return distance + + +def calc_tree_diameter(v_num, adj_table): +    # init x -> bridge vertex +    init_v = 0 +    distance_1 = graph_bfs(init_v, v_num, adj_table) +    diameter, bridge_v = 0, 0 +    for v, dis_1 in enumerate(distance_1): +        if isinf(dis_1): +            continue +        elif diameter < dis_1: +            diameter = dis_1 +            bridge_v = v +    # bridge vertex -> y +    distance_2 = graph_bfs(bridge_v, v_num, adj_table) +    diameter = 0 +    for dis_2 in distance_2: +        if isinf(dis_2): +            continue +        diameter = max(diameter, dis_2) +    return diameter + + +def solve(): +    _input = sys.stdin.readlines() +    v_num = int(_input[0]) +    edges = [x.split() for x in _input[1:]] +    init_adj_table = tuple(dict() for _ in range(v_num)) +    adj_table = generate_adj_table(edges, init_adj_table) +    ans = calc_tree_diameter(v_num, adj_table) +    print(ans) -def solve(): -    graph_bfs(init_v) -    diameter, bridge_v = 0, 0 -    for v in range(vertices): -        if isinf(distance[v]): -            continue -        if diameter < distance[v]: -            diameter = distance[v] -            bridge_v = v -    graph_bfs(bridge_v) -    diameter = 0 -    for v in range(vertices): -        if isinf(distance[v]): -            continue -        diameter = max(diameter, distance[v]) -    return diameter - - -    _input = sys.stdin.readlines() -    vertices = int(_input[0]) -    v_info = [x.split() for x in _input[1:]] -    queue = deque() -    distance = [float("inf")] * vertices -    init_adj_table = tuple(dict() for _ in range(vertices)) -    adj_table = generate_adj_table(v_info) -    init_v = 0 -    print((solve())) +    solve()