#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int n, p[1503], tp[1503], ans = 2147483647;
char v[1503][5];
int main() {
n = 1000;
for(int i=1; i<=n; i++) {
p[i]=127*i;
v[i][1] = 'A'; v[i][2] = 'B'; v[i][3] = rand()%6?'C':0;
}
int x;
for(int i=1; i<=n; i++) {
x = 0;
if(v[i][1] == 'A' || v[i][2] == 'A' || v[i][3] == 'A') {
x += p[i];
int tmp1 = p[i];
p[i] = 0;
for(int j=1; j<=n; j++) {
if(v[j][1] == 'B' || v[j][2] == 'B' || v[j][3] == 'B') {
x += p[j];
int tmp2 = p[j];
p[j] = 0;
for(int k=1; k<=n; k++) {
if(v[k][1] == 'C' || v[k][2] == 'C' || v[k][3] == 'C') {
x += p[k];
ans = min(ans, x);
x -= p[k];
}
}
p[j] = tmp2;
x -= p[j];
}
}
p[i] = tmp1;
x -= p[i];
}
}
printf("%d", ans == 2147483647 ? -1 : ans);
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y3N0cmluZz4KI2luY2x1ZGUgPGNzdGRpbz4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKaW50IG4sIHBbMTUwM10sIHRwWzE1MDNdLCBhbnMgPSAyMTQ3NDgzNjQ3OwpjaGFyIHZbMTUwM11bNV07CmludCBtYWluKCkgewoJbiA9IDEwMDA7Cglmb3IoaW50IGk9MTsgaTw9bjsgaSsrKSB7CgkJcFtpXT0xMjcqaTsKCQl2W2ldWzFdID0gJ0EnOyB2W2ldWzJdID0gJ0InOyB2W2ldWzNdID0gcmFuZCgpJTY/J0MnOjA7Cgl9CglpbnQgeDsKCWZvcihpbnQgaT0xOyBpPD1uOyBpKyspIHsKCQl4ID0gMDsKCQlpZih2W2ldWzFdID09ICdBJyB8fCB2W2ldWzJdID09ICdBJyB8fCB2W2ldWzNdID09ICdBJykgewoJCQl4ICs9IHBbaV07CgkJCWludCB0bXAxID0gcFtpXTsKCQkJcFtpXSA9IDA7CgkJCWZvcih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