// icebear
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 2277;
const int base = 311;
const int N = 50000 + 5;
int n, power[N];
char c[N];
vector<int> G[N];
int sz[N];
bool isCentroid[N];
void dfs(int u, int par) {
sz[u] = 1;
for(int v : G[u]) if (v != par && !isCentroid[v]) {
dfs(v, u);
sz[u] += sz[v];
}
}
int findCentroid(int u, int par, const int &subtree) {
for(int v : G[u]) if (v != par && !isCentroid[v] && sz[v] * 2 > subtree)
return findCentroid(v, u, subtree);
return u;
}
int pref[N], suff[N], depth[N];
unordered_map<int, bool> hsh[N];
int length, maxLen;
bool calcAns(int u, int par, bool answer) {
depth[u] = depth[par] + 1;
if (depth[u] > length) return false;
maxLen = max(maxLen, depth[u]);
pref[u] = (1LL * pref[par] * base + c[u]) % MOD;
suff[u] = (suff[par] + 1LL * c[u] * power[depth[u] - 1]) % MOD;
if (answer) {
if (pref[u] == suff[u] && depth[u] >= length && depth[u] % 2 == length % 2)
return true;
// pref[B] + suff[A] * T^len(B) = pref[A] + suff[B] * T^len(A)
int curHash = (1LL * suff[u] * power[length - depth[u]] - pref[u] + MOD) % MOD;
if (hsh[length - depth[u]].find(curHash) != hsh[length - depth[u]].end())
return true;
} else {
int curHash = (1LL * suff[u] * power[length - depth[u]] - pref[u] + MOD) % MOD;
hsh[depth[u]][curHash] = true;
}
for(int v : G[u]) if (v != par && !isCentroid[v]) {
if (calcAns(v, u, answer)) return true;
}
return false;
}
bool buildCentroid(int u) {
dfs(u, -1);
int centroid = findCentroid(u, -1, sz[u]);
isCentroid[centroid] = true;
maxLen = 0;
for(int v : G[centroid]) if (!isCentroid[v]) {
pref[centroid] = suff[centroid] = c[centroid];
depth[centroid] = 1;
if (calcAns(v, centroid, true)) return true;
pref[centroid] = suff[centroid] = 0;
depth[centroid] = 0;
calcAns(v, centroid, false);
}
for(int i = 0; i <= maxLen; i++) hsh[i].clear();
for(int v : G[centroid]) if (!isCentroid[v] && buildCentroid(v))
return true;
return false;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++) cin >> c[i];
for(int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
power[0] = 1;
for(int i = 1; i <= n; i++) power[i] = 1LL * power[i - 1] * base % MOD;
int low = 1, high = n / 2, res = 0;
while(low <= high) {
int mid = (low + high) >> 1;
length = 2 * mid;
for(int i = 1; i <= n; i++) isCentroid[i] = false;
if (buildCentroid(1)) res = length, low = mid + 1;
else high = mid - 1;
}
low = 0, high = (n - 1) / 2;
while(low <= high) {
int mid = (low + high) >> 1;
length = 2 * mid + 1;
for(int i = 1; i <= n; i++) isCentroid[i] = false;
if (buildCentroid(1)) res = max(res, length), low = mid + 1;
else high = mid - 1;
}
cout << res;
return 0;
}