/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
//package javaapplication1;
import java.io;
/**
*
* @author adahyvar
*/
public class JavaApplication1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
IO io = new IO();
String n = io.next();
if (n.length()%3 == 0) {
System.out.println(etaisyysSeiskasta(n));
} else {
System.out.println(etaisyysKaikista(n));
}
}
public static int etaisyysKaikista(String n) {
int huonot1 = 0;
int huonot2 = 0;
int huonot3 = 0;
int huonot4 = 0;
int huonot5 = 0;
int huonot6 = 0;
int huonot7 = 0;
for (int i = 0; i < n.length(); i++) {
if (n.charAt(i) != 0 && n.charAt(i) != 7) {
huonot7++;
} else if (n.charAt(i) != 1 && n.charAt(i) != 8 ) {
huonot1++;
} else if (n.charAt(i) != 2 && n.charAt(i) != 9) {
huonot2++;
} else if (n.charAt(i) != 3) {
huonot3++;
} else if (n.charAt(i) != 5) {
huonot5++;
} else if (n.charAt(i) != 4) {
huonot4++;
} else if (n.charAt(i) != 6) {
huonot6++;
}
}
int min1 = Math.min(huonot3 + huonot4, huonot2 + huonot5);
int min2 = Math.min(huonot1 + huonot6, huonot7);
int min3 = Math.min(min1, min2);
return min3;
}
public static int etaisyysSeiskasta(String n) {
int huonot = 0;
for (int i = 0; i < n.length(); i++) {
if (n.charAt(i) != 0 && n.charAt(i) != 7) {
huonot++;
}
}
return huonot;
}
}