import numpy as np

from numba import njit

from numba.types import Array, int64

i4 = np.int32

i8 = np.int64





@njit((Array(int64, 1, 'C'), Array(int64, 1, 'C'), int64, int64), cache=True)

def solve(a, b, H, W):

    ret = np.empty(H, i8)

    for i in range(H):

        ret[i] = -1

    dp0 = np.arange(W)

    dp = np.arange(W)

    arg0 = W - 1

    arg = 0

    for i in range(H):

        r1 = np.searchsorted(dp, a[i])

        r2 = np.searchsorted(dp, b[i])

        if b[i] == W:

            if r1 == 0:

                return ret

            else:

                dp0 = dp0[: r1]

                dp = dp[: r1]

        else:

            dp[r1:r2] = b[i]



        if arg0 > -1:

            if r1 <= arg0 < r2:

                for j in range(r1 - 1, -1, -1):

                    if dp[j] == dp0[j]:

                        arg0 = j

                        ret[i] = i + 1

                        break

                else:

                    arg0 = -1

                    arg = np.argmin(dp - dp0)

                    ret[i] = dp[arg] - dp0[arg] + i + 1

            else:

                ret[i] = i + 1

        else:

            if r1 <= arg < r2:

                arg = np.argmin(dp - dp0)

            ret[i] = dp[arg] - dp0[arg] + i + 1



        if i % 1000 == 999:

            m = 0

            prev = dp[0]

            for k in range(1, dp.shape[0]):

                if dp[k] > prev:

                    dp0[m] = dp0[k - 1]

                    dp[m] = prev

                    m += 1

                    prev = dp[k]

            else:

                dp0[m] = dp0[dp0.shape[0] - 1]

                dp[m] = prev

                m += 1

            dp0 = dp0[: m]

            dp = dp[: m]

            if arg0 > -1:

                for j in range(m - 1, -1, -1):

                    if dp[j] == dp0[j]:

                        arg0 = j

                        break

            else:

                arg = np.argmin(dp - dp0)

    return ret





def main(in_file):

    stdin = open(in_file)

    H, W = [int(x) for x in stdin.readline().split()]

    ab = np.fromstring(stdin.read(), i8, sep=' ').reshape((-1, 2)).T

    ans = solve(ab[0] - 1, ab[1].copy(), H, W)

    p = '\n'.join([str(x) for x in ans.tolist()])

    print(p)





if __name__ == '__main__':

    main('/dev/stdin')
import numpy as np

i4 = np.int32

i8 = np.int64





def solve(a, b, H, W):

    ret = np.empty(H, i8)

    for i in range(H):

        ret[i] = -1

    dp0 = np.arange(W)

    dp = np.arange(W)

    arg0 = W - 1

    arg = 0

    for i in range(H):

        r1 = np.searchsorted(dp, a[i])

        r2 = np.searchsorted(dp, b[i])

        if b[i] == W:

            if r1 == 0:

                return ret

            else:

                dp0 = dp0[: r1]

                dp = dp[: r1]

        else:

            dp[r1:r2] = b[i]



        if arg0 > -1:

            if r1 <= arg0 < r2:

                for j in range(r1 - 1, -1, -1):

                    if dp[j] == dp0[j]:

                        arg0 = j

                        ret[i] = i + 1

                        break

                else:

                    arg0 = -1

                    arg = np.argmin(dp - dp0)

                    ret[i] = dp[arg] - dp0[arg] + i + 1

            else:

                ret[i] = i + 1

        else:

            if r1 <= arg < r2:

                arg = np.argmin(dp - dp0)

            ret[i] = dp[arg] - dp0[arg] + i + 1



        if i % 1000 == 999:

            m = 0

            prev = dp[0]

            for k in range(1, dp.shape[0]):

                if dp[k] > prev:

                    dp0[m] = dp0[k - 1]

                    dp[m] = prev

                    m += 1

                    prev = dp[k]

            else:

                dp0[m] = dp0[dp0.shape[0] - 1]

                dp[m] = prev

                m += 1

            dp0 = dp0[: m]

            dp = dp[: m]

            if arg0 > -1:

                for j in range(m - 1, -1, -1):

                    if dp[j] == dp0[j]:

                        arg0 = j

                        break

            else:

                arg = np.argmin(dp - dp0)

    return ret





def main(in_file):

    stdin = open(in_file)

    H, W = [int(x) for x in stdin.readline().split()]

    ab = np.fromstring(stdin.read(), i8, sep=' ').reshape((-1, 2)).T.copy()

    ans = solve(ab[0] - 1, ab[1], H, W)

    p = '\n'.join([str(x) for x in ans.tolist()])

    print(p)





def cc_export():

    from numba.pycc import CC

    from numba.types import Array, int64

    cc = CC('my_module')

    cc.export('solve', (Array(int64, 1, 'C'), Array(int64, 1, 'C'), int64, int64))(solve)

    cc.compile()





if __name__ == '__main__':

    import sys

    if sys.argv[-1] == 'ONLINE_JUDGE':

        cc_export()

        exit(0)

    from my_module import solve

    main('/dev/stdin')
import numpy as np
from numba import njit
from numba.types import Array, int64

i4 = np.int32
i8 = np.int64


@njit((Array(int64, 1, "C"), Array(int64, 1, "C"), int64, int64), cache=True)
def solve(a, b, H, W):
    ret = np.empty(H, i8)
    for i in range(H):
        ret[i] = -1
    dp0 = np.arange(W)
    dp = np.arange(W)
    arg0 = W - 1
    arg = 0
    for i in range(H):
        r1 = np.searchsorted(dp, a[i])
        r2 = np.searchsorted(dp, b[i])
        if b[i] == W:
            if r1 == 0:
                return ret
            else:
                dp0 = dp0[:r1]
                dp = dp[:r1]
        else:
            dp[r1:r2] = b[i]
        if arg0 > -1:
            if r1 <= arg0 < r2:
                for j in range(r1 - 1, -1, -1):
                    if dp[j] == dp0[j]:
                        arg0 = j
                        ret[i] = i + 1
                        break
                else:
                    arg0 = -1
                    arg = np.argmin(dp - dp0)
                    ret[i] = dp[arg] - dp0[arg] + i + 1
            else:
                ret[i] = i + 1
        else:
            if r1 <= arg < r2:
                arg = np.argmin(dp - dp0)
            ret[i] = dp[arg] - dp0[arg] + i + 1
        if i % 1000 == 999:
            m = 0
            prev = dp[0]
            for k in range(1, dp.shape[0]):
                if dp[k] > prev:
                    dp0[m] = dp0[k - 1]
                    dp[m] = prev
                    m += 1
                    prev = dp[k]
            else:
                dp0[m] = dp0[dp0.shape[0] - 1]
                dp[m] = prev
                m += 1
            dp0 = dp0[:m]
            dp = dp[:m]
            if arg0 > -1:
                for j in range(m - 1, -1, -1):
                    if dp[j] == dp0[j]:
                        arg0 = j
                        break
            else:
                arg = np.argmin(dp - dp0)
    return ret


def main(in_file):
    stdin = open(in_file)
    H, W = [int(x) for x in stdin.readline().split()]
    ab = np.fromstring(stdin.read(), i8, sep=" ").reshape((-1, 2)).T
    ans = solve(ab[0] - 1, ab[1].copy(), H, W)
    p = "\n".join([str(x) for x in ans.tolist()])
    print(p)


if __name__ == "__main__":
    main("/dev/stdin")

import numpy as np

i4 = np.int32
i8 = np.int64


def solve(a, b, H, W):
    ret = np.empty(H, i8)
    for i in range(H):
        ret[i] = -1
    dp0 = np.arange(W)
    dp = np.arange(W)
    arg0 = W - 1
    arg = 0
    for i in range(H):
        r1 = np.searchsorted(dp, a[i])
        r2 = np.searchsorted(dp, b[i])
        if b[i] == W:
            if r1 == 0:
                return ret
            else:
                dp0 = dp0[:r1]
                dp = dp[:r1]
        else:
            dp[r1:r2] = b[i]
        if arg0 > -1:
            if r1 <= arg0 < r2:
                for j in range(r1 - 1, -1, -1):
                    if dp[j] == dp0[j]:
                        arg0 = j
                        ret[i] = i + 1
                        break
                else:
                    arg0 = -1
                    arg = np.argmin(dp - dp0)
                    ret[i] = dp[arg] - dp0[arg] + i + 1
            else:
                ret[i] = i + 1
        else:
            if r1 <= arg < r2:
                arg = np.argmin(dp - dp0)
            ret[i] = dp[arg] - dp0[arg] + i + 1
        if i % 1000 == 999:
            m = 0
            prev = dp[0]
            for k in range(1, dp.shape[0]):
                if dp[k] > prev:
                    dp0[m] = dp0[k - 1]
                    dp[m] = prev
                    m += 1
                    prev = dp[k]
            else:
                dp0[m] = dp0[dp0.shape[0] - 1]
                dp[m] = prev
                m += 1
            dp0 = dp0[:m]
            dp = dp[:m]
            if arg0 > -1:
                for j in range(m - 1, -1, -1):
                    if dp[j] == dp0[j]:
                        arg0 = j
                        break
            else:
                arg = np.argmin(dp - dp0)
    return ret


def main(in_file):
    stdin = open(in_file)
    H, W = [int(x) for x in stdin.readline().split()]
    ab = np.fromstring(stdin.read(), i8, sep=" ").reshape((-1, 2)).T.copy()
    ans = solve(ab[0] - 1, ab[1], H, W)
    p = "\n".join([str(x) for x in ans.tolist()])
    print(p)


def cc_export():
    from numba.pycc import CC
    from numba.types import Array, int64

    cc = CC("my_module")
    cc.export("solve", (Array(int64, 1, "C"), Array(int64, 1, "C"), int64, int64))(
        solve
    )
    cc.compile()


if __name__ == "__main__":
    import sys

    if sys.argv[-1] == "ONLINE_JUDGE":
        cc_export()
        exit(0)
    from my_module import solve

    main("/dev/stdin")

-from numba import njit -from numba.types import Array, int64 -@njit((Array(int64, 1, "C"), Array(int64, 1, "C"), int64, int64), cache=True) -    ab = np.fromstring(stdin.read(), i8, sep=" ").reshape((-1, 2)).T -    ans = solve(ab[0] - 1, ab[1].copy(), H, W) +    ab = np.fromstring(stdin.read(), i8, sep=" ").reshape((-1, 2)).T.copy() +    ans = solve(ab[0] - 1, ab[1], H, W) +def cc_export(): +    from numba.pycc import CC +    from numba.types import Array, int64 + +    cc = CC("my_module") +    cc.export("solve", (Array(int64, 1, "C"), Array(int64, 1, "C"), int64, int64))( +        solve +    ) +    cc.compile() + + +    import sys + +    if sys.argv[-1] == "ONLINE_JUDGE": +        cc_export() +        exit(0) +    from my_module import solve +