import math


def getNumberOfNodesUsed(p):
    """
    Given a permutation p, with possible unassigned cores, denoted with -1,
    extract the number of compute nodes and processes per node used, by counting
    the number of non-minus-ones on the first node to get the processes per node
    and the divide length of p by computed processes per node. Assumes that the
    ranks on the first node are the first entries in p
    :param p: permutation list of ranks with possible unassigned processes (denoted with -1)
    :return: number of used compute nodes and processes per node
    """
    ppn = 0
    index = 0
    #skip the -1 ones
    while index < len(p):
        if p[index] >= 0:
            break
        index += 1
    #count processes on first node
    while index < len(p):
        if p[index] < 0:
            break
        ppn += 1
        index += 1
    totalP = ppn
    #count total processes
    while index < len(p):
        if p[index] >= 0:
            totalP += 1
        index += 1
    n_nodes = math.ceil(totalP / ppn)
    return (n_nodes, ppn)