PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GL0000056. Chỉ số của phân số (A Rational Sequence 2)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Chỉ số của phân số (A Rational Sequence 2)

A Rational Sequence 2

Source: Kattis

Version: Phuoc Hung OJ Extended

Problem Statement

In the infinite binary tree rooted at 1/1, the children of p/q are p/(p+q) and (p+q)/q. Number all vertices in breadth-first, left-to-right order, starting at 1. Given a reduced fraction p/q, find its one-based position n in that order.

Input

A single reduced positive fraction p/q, without a dataset identifier.

Output

The one-based integer index n.

Subtasks

  • Subtask 1 (20 points): p,qp,q are positive coprime integers with p/q=F(n)p/q=F(n) for 1≤n≤10231\le n\le 1023; the index nn is not given in the input.
  • Subtask 2 (30 points): p,qp,q are positive coprime integers with p/q=F(n)p/q=F(n) for 1≤n≤1061\le n\le 10^6; the index nn is not given in the input.
  • Subtask 3 (50 points): p,qp,q are positive coprime integers with p/q=F(n)p/q=F(n) for 1≤n≤231−11\le n\le 2^{31}-1; the index nn is not given in the input.

Examples

Input

5/2

Output

11

Explanation

The binary index of 5/2 is 1011, which equals 11 in decimal.