#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
typedef struct Node
{
char typeChar;
int rate;
Node *leftChild;
Node *rightChild;
}charNode;
char *inputStr;
Node nodes[200 + 199];
int total;
int depth;
int getNum(char *in, char c)
{
int times = 0;
for (int i = 0; i <= strlen(in); i++)
{
if (*(in + i) == c) times++;
}
return times;
}
int getTypeChar(char *in)
{
inputStr = (char *)malloc(sizeof(char)*200);
memset(inputStr, '\0', sizeof(inputStr));
int p = 0;
bool isAdded;
for (int i = 0; i<strlen(in); i++)
{
isAdded = false;
if (p == 0)
{
*inputStr = *in;
nodes[p].typeChar = *inputStr;
nodes[p].rate = getNum(in, *in);
nodes[p].leftChild = NULL;
nodes[p].rightChild = NULL;
p++;
continue;
}
for (int j = 0; j<p; j++)
{
if (*(inputStr + j) == *(in + i))
{
isAdded = true;
break;
}
}
if (!isAdded)
{
inputStr[p] = *(in + i);
nodes[p].typeChar = *(inputStr + p);
nodes[p].rate = getNum(in, *(in + i));
nodes[p].leftChild = NULL;
nodes[p].rightChild = NULL;
p++;
}
}
return p;
}
bool compNode(const Node & node1, const Node & node2)
{
if (node1.rate <= node2.rate)
{
return true;
}
else
{
return false;
}
}
void huf(int startIndex, int endIndex, int length)
{
sort(nodes + startIndex, nodes + startIndex + length, compNode);
nodes[endIndex + 1].rate = nodes[startIndex].rate + nodes[startIndex+1].rate;
nodes[endIndex + 1].leftChild = &nodes[startIndex];
nodes[endIndex + 1].rightChild = &nodes[startIndex+1];
if (endIndex + 1 != total)
{
huf(startIndex + 2, endIndex + 1, length - 1);
}
}
int depthFirst(charNode *);
int getCodeLength(int lastIndex)
{
charNode *root = &nodes[lastIndex];
depth = -1;
return depthFirst(root);
}
int depthFirst(charNode *node)
{
depth++;
if (node->leftChild == NULL && node->rightChild == NULL)
{
return node->rate * depth;
}
else
{
int leftSize = depthFirst(node->leftChild);
depth--;
int rightSize = depthFirst(node->rightChild);
depth--;
return leftSize + rightSize;
}
}
int main()
{
char *in;
int num;
in = (char *)malloc(sizeof(char) * 1000);
while(scanf("%s", in) != EOF)
{
num = getTypeChar(in);
if(num == 1)
{
cout<<strlen(in)<<endl;
continue;
}
total = num * 2 - 1;
huf(0, num - 1, num);
cout<<getCodeLength(total-1)<<endl;
}
return 0;
}