PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FW0000067. Bưu tá lại rung chuông (The Postal Worker Rings Once)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Bưu tá lại rung chuông (The Postal Worker Rings Once)

The Postal Worker Rings Once

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

Street names encode their endpoints as their first and last lowercase letters, and the street cost is its length. The undirected street network is connected and has at most two odd-degree vertices. Find the shortest closed postman walk traversing every street at least once.

Input

First m; then m lowercase street names of length between 2 and 50 with differing first and last characters.

Output

Print the total length of the minimum closed postman route.

Subtasks

  • Subtask 1 (40 points): 3≤m≤12, 2≤∣s∣≤203\le m\le 12,\ 2\le |s|\le 20. Street names consist only of lowercase English letters. The network is connected and has at most two odd-degree vertices.
  • Subtask 2 (60 points): 3≤m≤100, 2≤∣s∣≤503\le m\le 100,\ 2\le |s|\le 50. Street names consist only of lowercase English letters. The network is connected and has at most two odd-degree vertices.

Examples

Input

3
ab
bc
ca

Output

6

Explanation

The three length-2 streets form an Eulerian triangle; walking each exactly once and returning to the start costs 2+2+2=6.