import java.util.; import java.io.; public class Main{ public static void main(String[] args)throws IOException{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String str = null; while((str = in.readLine()) != null){ int num = Integer.parseInt(str); int sum = 0; if(num == 0) break; while(num > 2){ int temp = num % 3; num /= 3; sum += num; num += temp; } if(num == 2) sum ++; System.out.println(sum); } } }